Fixed self and added tests back

This commit is contained in:
2026-05-21 08:09:26 +02:00
parent 7688599504
commit 702af9da3b
8 changed files with 228 additions and 18 deletions
+95
View File
@@ -0,0 +1,95 @@
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;
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);
}
}
}
}
+7
View File
@@ -0,0 +1,7 @@
pub enum Event {
Startup,
Shutdown,
TaskExecuted(String),
PluginLoaded(String),
Error(String),
}
+78
View File
@@ -0,0 +1,78 @@
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);
}
}
}
+10 -15
View File
@@ -1,17 +1,12 @@
pub mod app;
pub mod task;
pub mod plugin;
pub mod logger;
pub mod event;
void main() {
i32 mut x = 10;
var mut app_instance = new app::Application("mist-app");
x = 1 + 1;
x++;
if (1 == 1) {
println!("Yes!");
}
i32 y = if (x == 3) { 1 } else { 0 };
println!("hello, world {x}");
println!("{y}")
}
app_instance.boot();
app_instance.run();
}
+16
View File
@@ -0,0 +1,16 @@
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;
}
}
+20
View File
@@ -0,0 +1,20 @@
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,
};
}
+1 -2
View File
@@ -29,7 +29,6 @@ keyword = {
| "true"
| "false"
| "dyn"
| "self"
}
identifier = @{
@@ -343,7 +342,7 @@ block = {
}
statement_list = {
(statement | (!expr ~ unexpected_statement))*
(statement | (unexpected_statement))*
}
statement = _{
+1 -1
View File
@@ -78,7 +78,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!(
Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::unexpected_statement => ast_expr!(
Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
),