Rewriting statement system for expr statements
This commit is contained in:
@@ -5,6 +5,6 @@ edition = "2024"
|
|||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "main"
|
name = "main"
|
||||||
path = "build/main.rs"
|
path = ".mist/src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
use <super::task>;
|
|
||||||
use <super::logger>;
|
|
||||||
use <super::event>;
|
|
||||||
|
|
||||||
pub class Application {
|
|
||||||
String name;
|
|
||||||
|
|
||||||
Vec<task::Task> tasks;
|
|
||||||
Vec<String> plugins;
|
|
||||||
|
|
||||||
logger::Logger logger;
|
|
||||||
|
|
||||||
bool running;
|
|
||||||
i32 ticks;
|
|
||||||
|
|
||||||
pub constructor(str* name) {
|
|
||||||
self.name = name.to_string();
|
|
||||||
|
|
||||||
self.tasks = new Vec();
|
|
||||||
self.plugins = new Vec();
|
|
||||||
|
|
||||||
self.logger = new logger::Logger("app");
|
|
||||||
|
|
||||||
self.running = false;
|
|
||||||
self.ticks = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void boot(self mut*) {
|
|
||||||
self.logger.info("booting application");
|
|
||||||
|
|
||||||
self.running = true;
|
|
||||||
|
|
||||||
self.emit(event::Event::Startup);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void run(self mut*) {
|
|
||||||
while(self.running) {
|
|
||||||
self.tick();
|
|
||||||
|
|
||||||
if(self.ticks >= 5) {
|
|
||||||
self.shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void tick(self mut*) {
|
|
||||||
self.logger.info("tick");
|
|
||||||
|
|
||||||
self.ticks++;
|
|
||||||
|
|
||||||
var task = task::create_task("background-job");
|
|
||||||
|
|
||||||
self.tasks.push(task);
|
|
||||||
|
|
||||||
println!("tasks: {}", self.tasks.len());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void shutdown(self mut*) {
|
|
||||||
self.emit(event::Event::Shutdown);
|
|
||||||
|
|
||||||
self.logger.warning("shutdown");
|
|
||||||
|
|
||||||
self.running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void register_plugin(self mut*, String name) {
|
|
||||||
self.plugins.push(name.clone());
|
|
||||||
|
|
||||||
self.emit(event::Event::PluginLoaded(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
void emit(self*, event::Event event) {
|
|
||||||
match(event) {
|
|
||||||
event::Event::Startup => {
|
|
||||||
self.logger.info("startup event");
|
|
||||||
}
|
|
||||||
|
|
||||||
event::Event::Shutdown => {
|
|
||||||
self.logger.info("shutdown event");
|
|
||||||
}
|
|
||||||
|
|
||||||
event::Event::PluginLoaded(name) => {
|
|
||||||
println!("plugin loaded: {}", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
event::Event::TaskExecuted(name) => {
|
|
||||||
println!("task executed: {}", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
event::Event::Error(message) => {
|
|
||||||
self.logger.error(&message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
pub enum Event {
|
|
||||||
Startup,
|
|
||||||
Shutdown,
|
|
||||||
TaskExecuted(String),
|
|
||||||
PluginLoaded(String),
|
|
||||||
Error(String),
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
use <std::fmt::Formatter>;
|
|
||||||
use <std::fmt>;
|
|
||||||
|
|
||||||
pub trait TestLogging<T: fmt::Display>: fmt::Display {
|
|
||||||
String get(self*);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum LogLevel {
|
|
||||||
Info,
|
|
||||||
Warning,
|
|
||||||
Error,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for LogLevel {
|
|
||||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
|
||||||
match (self) {
|
|
||||||
LogLevel::Info => {
|
|
||||||
return write!(f, "[INFO]");
|
|
||||||
}
|
|
||||||
|
|
||||||
LogLevel::Warning => {
|
|
||||||
return write!(f, "[WARN]");
|
|
||||||
}
|
|
||||||
|
|
||||||
LogLevel::Error => {
|
|
||||||
return write!(f, "[ERROR]");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestLogging<LogLevel> for LogLevel {
|
|
||||||
String get(self*) {
|
|
||||||
return format!("{self}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub class Logger {
|
|
||||||
String prefix;
|
|
||||||
|
|
||||||
pub constructor(str* prefix) {
|
|
||||||
self.prefix = prefix.to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void info(self*, str* message) {
|
|
||||||
self.log(LogLevel::Info, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void warning(self*, str* message) {
|
|
||||||
self.log(LogLevel::Warning, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub void error(self*, str* message) {
|
|
||||||
self.log(LogLevel::Error, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void log(self*, LogLevel level, str* message) {
|
|
||||||
match(level) {
|
|
||||||
LogLevel::Info => {
|
|
||||||
println!("{level} {} {}", self.prefix, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
LogLevel::Warning => {
|
|
||||||
println!("{} {} {}", level.get(), self.prefix, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
LogLevel::Error => {
|
|
||||||
println!("{level} {} {}", self.prefix, message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display {
|
|
||||||
fmt::Result fmt(self*, Formatter<'_> mut* f) {
|
|
||||||
return write!(f, "logger ({})", self.prefix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-14
@@ -1,15 +1,3 @@
|
|||||||
pub mod app;
|
|
||||||
pub mod task;
|
|
||||||
pub mod plugin;
|
|
||||||
pub mod logger;
|
|
||||||
pub mod event;
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
var mut app_instance = new app::Application("mist-app");
|
println!("hello, world");
|
||||||
|
}
|
||||||
// greeting::register(&mut app_instance);
|
|
||||||
// statistics::register(&mut app_instance);
|
|
||||||
|
|
||||||
app_instance.boot();
|
|
||||||
app_instance.run();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
pub struct PluginInfo {
|
|
||||||
pub String name,
|
|
||||||
pub String version,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub class PluginRegistry<T> {
|
|
||||||
T plugins;
|
|
||||||
|
|
||||||
pub constructor(T plugins) {
|
|
||||||
self.plugins = plugins;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub T plugins(self) {
|
|
||||||
return self.plugins;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
pub enum TaskState {
|
|
||||||
Pending,
|
|
||||||
Running,
|
|
||||||
Finished,
|
|
||||||
Failed,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Task {
|
|
||||||
pub String name,
|
|
||||||
pub TaskState state,
|
|
||||||
pub i32 executions,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub Task create_task(str* name) {
|
|
||||||
return Task {
|
|
||||||
name: name.to_string(),
|
|
||||||
state: TaskState::Pending,
|
|
||||||
executions: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
+21
-24
@@ -342,53 +342,49 @@ block = {
|
|||||||
"{" ~ (statement | unexpected_statement)* ~ "}"
|
"{" ~ (statement | unexpected_statement)* ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
statement = _{
|
statement = {
|
||||||
match_stmt
|
(expr | basic_stmt_left_recurse) ~ semicolon
|
||||||
| if_stmt
|
|
||||||
| while_stmt
|
|
||||||
| c_for_stmt
|
|
||||||
| for_stmt
|
|
||||||
| return_stmt
|
|
||||||
| break_stmt
|
|
||||||
| continue_stmt
|
|
||||||
| block
|
|
||||||
| increment_statement
|
|
||||||
| decrement_statement
|
|
||||||
| var_decl_statement
|
|
||||||
| assign_statement
|
|
||||||
| expr_stmt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
// BASIC STATEMENTS
|
// BASIC STATEMENTS
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|
||||||
expr_stmt = {
|
basic_stmt = {
|
||||||
expr ~ semicolon
|
return_stmt
|
||||||
|
| break_stmt
|
||||||
|
| continue_stmt
|
||||||
|
| var_decl_statement
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_stmt_left_recurse = {
|
||||||
|
increment_statement
|
||||||
|
| decrement_statement
|
||||||
|
| assign_statement
|
||||||
}
|
}
|
||||||
|
|
||||||
return_stmt = {
|
return_stmt = {
|
||||||
"return" ~ expr? ~ semicolon
|
"return" ~ expr?
|
||||||
}
|
}
|
||||||
|
|
||||||
break_stmt = {
|
break_stmt = {
|
||||||
"break" ~ semicolon
|
"break"
|
||||||
}
|
}
|
||||||
|
|
||||||
continue_stmt = {
|
continue_stmt = {
|
||||||
"continue" ~ semicolon
|
"continue"
|
||||||
}
|
}
|
||||||
|
|
||||||
var_decl_statement = {
|
var_decl_statement = {
|
||||||
var_decl ~ ("=" ~ expr)? ~ semicolon
|
var_decl ~ ("=" ~ expr)?
|
||||||
}
|
}
|
||||||
|
|
||||||
increment_statement = {
|
increment_statement = {
|
||||||
expr ~ "++" ~ semicolon
|
expr ~ "++"
|
||||||
}
|
}
|
||||||
|
|
||||||
decrement_statement = {
|
decrement_statement = {
|
||||||
expr ~ "--" ~ semicolon
|
expr ~ "--"
|
||||||
}
|
}
|
||||||
|
|
||||||
compound_assign = {
|
compound_assign = {
|
||||||
@@ -497,7 +493,8 @@ expr_path = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
primary = {
|
primary = {
|
||||||
literal
|
basic_stmt
|
||||||
|
| literal
|
||||||
| expr_path
|
| expr_path
|
||||||
| tuple
|
| tuple
|
||||||
| array
|
| array
|
||||||
|
|||||||
@@ -41,10 +41,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
|
|||||||
match rule {
|
match rule {
|
||||||
Rule::statement => Statement::try_from(inner.next().unwrap()),
|
Rule::statement => Statement::try_from(inner.next().unwrap()),
|
||||||
|
|
||||||
Rule::expr_stmt => {
|
|
||||||
ast_expr!(Statement::Expression(inner.next().unwrap().try_into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::block => ast_expr!(Statement::Block(pair.try_into())),
|
Rule::block => ast_expr!(Statement::Block(pair.try_into())),
|
||||||
|
|
||||||
Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())),
|
Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())),
|
||||||
|
|||||||
Reference in New Issue
Block a user