feat: Implement lexer and parser for AST
Introduces the lexer and parser modules, enabling the conversion of source code into an Abstract Syntax Tree (AST). This includes: - Defining token kinds and structures. - Implementing lexer logic to tokenize input. - Defining AST node kinds and structures. - Implementing parser logic to construct the AST from tokens. - Adding support for basic expressions, lists, keywords, and identifiers. - Adding the `lazy_static` dependency for keyword interning. - Refactoring `Value::Nil` to `Value::Void`.
This commit is contained in:
+22
-31
@@ -40,40 +40,31 @@ impl eframe::App for CompilerApp {
|
||||
|
||||
// Compile Button (at the top of the bottom panel)
|
||||
if ui.button("Compile").clicked() {
|
||||
use crate::ast::*;
|
||||
use std::sync::Arc;
|
||||
use crate::ast::parser::Parser;
|
||||
use crate::ast::nodes::Context;
|
||||
|
||||
// 1. Create shared identity
|
||||
let identity = Arc::new(NodeIdentity { line: 1, col: 1 });
|
||||
|
||||
// 2. Build AST: (if true 42 nil)
|
||||
let ast = Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::If {
|
||||
cond: Box::new(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Constant(Value::Bool(true)),
|
||||
}),
|
||||
then_br: Box::new(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Constant(Value::Int(42)),
|
||||
}),
|
||||
else_br: Some(Box::new(Node {
|
||||
identity: identity.clone(),
|
||||
kind: UntypedKind::Constant(Value::Nil),
|
||||
})),
|
||||
},
|
||||
let mut parser = match Parser::new(&self.source_code) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
self.output_log = format!("Lexer Error: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 3. Evaluate
|
||||
let mut context = Context {};
|
||||
let result = ast.eval(&mut context);
|
||||
|
||||
self.output_log = format!(
|
||||
"AST Test:\nSource: (if true 42 nil)\nResult: {}\n\nCompiler log:\nStarted at {:?}",
|
||||
result,
|
||||
std::time::SystemTime::now(),
|
||||
);
|
||||
match parser.parse_expression() {
|
||||
Ok(ast) => {
|
||||
let mut context = Context {};
|
||||
let result = ast.eval(&mut context);
|
||||
self.output_log = format!(
|
||||
"AST Parsed Successfully.\nResult: {}\n\nFinished at {:?}",
|
||||
result,
|
||||
std::time::SystemTime::now(),
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
self.output_log = format!("Parser Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui.add_space(5.0);
|
||||
|
||||
Reference in New Issue
Block a user