Add type system and refator for having only epression
This commit is contained in:
Giulio Agostini
2026-07-06 10:43:17 +02:00
parent 9f15a00b98
commit 842216729b
23 changed files with 940 additions and 979 deletions
Executable → Regular
View File
Executable
+36
View File
@@ -0,0 +1,36 @@
#+title: README
* Syntax example:
#+begin_src
funzione :: fn(parametro1: Number, parametro2: String): String do
end
#+end_src
* Planning:
allora quello che devo fare è:
** TODO Controllare se ho finito la questione del retourn:labe
** TODO implement struct:
#+begin_src rlox
Cosa :: struct{
field1: Number
}
#+end_src
*** TODO update lexert
*** TODO update ast
*** TODO update parser
** TODO implement partial function ...
#+begin_src rlox
cosa := make(Cosa, feld1: 42)
function :: fn(self:Cosa, b: Number) -> Number
res := function(cosa,44)
#+end_src
*** TODO ... to undericlty create method
#+begin_src rlox
res := cosa.function(44)
#+end_src
*** TODO ... to make pipeline
#+begin_src rlox
res := cosa |> function(44)
#+end_src
** TODO Understand what is a type system
+6 -6
View File
@@ -1,12 +1,12 @@
closure :: fn() do closure :: fn(): Any do
value := "Closure"; value := "Closure";
internal :: fn() do internal :: fn(): String do
print value; print value;
return value; return value;
end end;
return internal; return internal;
end end;
internal := closure(); internal := closure();
internal(); internal();
@@ -17,9 +17,9 @@ c := b;
c = b = a; c = b = a;
d := c = b = a; d := c = b = a;
do do
showA :: fn() do showA :: fn(): Nil do
print a; print a;
end end;
showA(); showA();
a := "Local"; a := "Local";
+3 -3
View File
@@ -4,7 +4,7 @@
// address: String, // address: String,
// } // }
//func_name :: fn(param: Number, param2: String) {param is Int} do //func_name :: fn(param: Number, param2: String) {param is Int} do
func_name :: fn(param: Int, param2: String) do func_name :: fn(param: Int, param2: String): Any do
print param; print param;
print param2; print param2;
while param < param2 do while param < param2 do
@@ -26,6 +26,6 @@ func_name :: fn(param: Int, param2: String) do
end end
return False or True; return False or True;
end end;
func_name(1,20) func_name(1,20);
+14 -14
View File
@@ -1,30 +1,30 @@
function :: fn() do function :: fn(): Nil do
print "Function"; print "Function";
end end;
function_factory :: fn() do function_factory :: fn(): Any do
print "Function Factory"; print "Function Factory";
function function;
end end;
function_fatcoty_factory :: fn() do function_fatcoty_factory :: fn(): Any do
print "Function Factory Factory"; print "Function Factory Factory";
return function_factory; return function_factory;
end end;
//function(); //function();
//function_factory()(); //function_factory()();
//function_fatcoty_factory()()(); //function_fatcoty_factory()()();
clock() clock();
closure :: fn() do closure :: fn(): Any do
value = "Closure"; value := "Closure";
internal :: fn() do internal :: fn(): Nil do
print value; print value;
end end;
return internal; return internal;
end end;
closure() closure();
+57
View File
@@ -0,0 +1,57 @@
statement -> print_statement
| return_statement
| break_statement
| block_statement
| var_statement
| if_statement
| while_statement
| for_statement
| expression_statement
print_statement -> "print" expression ";"
return_statement -> "return" expression? ";"
break_statement -> "break" ";"
block_statement -> "do" statement* "end"
expression_statement -> expression ";"?
; leading "var" optional; the ":" is required
var_statement -> "var"? IDENTIFIER ":" IDENTIFIER?
( variable_declaration
| function_declaration
| struct_declaration ) ; struct = WIP
variable_declaration -> ( "=" expression )? ";"
function_declaration -> ":" "fn" "(" parameters? ")" ( "{" expression "}" )? statement
parameters -> IDENTIFIER ( ":" IDENTIFIER )?
( "," IDENTIFIER ( ":" IDENTIFIER )? )*
if_statement -> "if" expression "then" statement
( "elif" expression "then" statement )*
( "else" statement )?
while_statement -> "while" expression statement
for_statement -> "for" var_statement expression ";" expression_statement statement
; ---------- expressions (lowest -> highest precedence) ----------
expression -> assignment
assignment -> IDENTIFIER "=" assignment ; right-associative
| pipe
pipe -> logic ( "|>" logic )* ; left-assoc; x |> f(a) == f(x, a)
logic -> is ( ( "or" | "and" ) is )*
is -> equality ( "is" equality )*
equality -> comparison ( ( "==" | "!=" ) comparison )*
comparison -> term ( ( ">" | ">=" | "<" | "<=" ) term )*
term -> factor ( ( "+" | "-" ) factor )*
factor -> unary ( ( "*" | "/" ) unary )*
unary -> ( "!" | "-" ) unary | call
call -> primary ( "(" arguments? ")" )*
arguments -> expression ( "," expression )*
primary -> NUMBER | STRING | "true" | "false" | "nil"
| "(" expression ")" | IDENTIFIER
| dictionary | list | ATOM ; WIP
; ---------- work in progress (lexed; parser support being added) ----------
struct_declaration -> "struct" "{" field* "}"
field -> IDENTIFIER ( ":" IDENTIFIER )? ";"
dictionary -> "dict" "(" IDENTIFIER ":" expression
( "," IDENTIFIER ":" expression )* ")"
list -> "list" "(" expression ( "," expression )* ")"
+85 -111
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
backend::environment::EnvironmentStack, backend::environment::EnvironmentStack,
common::{ common::{
ast::{AstNode, Expr, NodeId, Stmt}, ast::{AstNode, Expr, NodeId},
base_value::{BaseValue, NativeFunction, Number, Truthy}, base_value::{BaseValue, NativeFunction, Number, Truthy},
lox_result::{runtime_error, LoxError, LoxResult}, lox_result::{runtime_error, LoxError, LoxResult},
}, },
@@ -21,18 +21,9 @@ pub trait EvaluateInterpreter<T> {
fn evaluate(&mut self, stmt: T) -> LoxResult<BaseValue>; fn evaluate(&mut self, stmt: T) -> LoxResult<BaseValue>;
} }
impl EvaluateInterpreter<AstNode<Stmt>> for Interpreter { impl EvaluateInterpreter<AstNode> for Interpreter {
fn evaluate(&mut self, node: AstNode<Stmt>) -> LoxResult<BaseValue> { fn evaluate(&mut self, node: AstNode) -> LoxResult<BaseValue> {
let stmt = node.node; self.eval_expr(&node)
let result = self.interpret_stmt_inner(stmt);
match result {
Ok(value) => Ok(value),
Err(LoxError::RuntimeError {
message,
source_slice,
}) if source_slice.is_synthetic() => runtime_error(node.source_slice.clone(), message),
Err(err) => Err(err),
}
} }
} }
@@ -63,9 +54,9 @@ impl Interpreter {
/// Evaluate an expression node, threading its `NodeId` so resolved /// Evaluate an expression node, threading its `NodeId` so resolved
/// variables and assignments can use their precomputed scope distance. /// variables and assignments can use their precomputed scope distance.
fn eval_expr(&mut self, node: &AstNode<Expr>) -> LoxResult<BaseValue> { fn eval_expr(&mut self, node: &AstNode) -> LoxResult<BaseValue> {
let result = match &node.node { let result = match &node.node {
Expr::Literal { value } => match value { Expr::Literal { value } => match &**value {
BaseValue::Function(func) => { BaseValue::Function(func) => {
let mut func = func.clone(); let mut func = func.clone();
func.closure = Some(self.enviorment.clone()); func.closure = Some(self.enviorment.clone());
@@ -93,6 +84,67 @@ impl Interpreter {
let value = self.eval_expr(value)?; let value = self.eval_expr(value)?;
self.assign_variable(name, node.id, value) self.assign_variable(name, node.id, value)
} }
Expr::Print { expression } => {
let value = self.eval_expr(expression)?;
println!("{}", value);
Ok(BaseValue::Nil)
}
Expr::VarDeclaration {
name, initializer, ..
} => {
let value = match initializer {
Some(expr_node) => self.eval_expr(expr_node)?,
None => BaseValue::Nil,
};
self.enviorment.declare(name.clone(), value)
}
Expr::Return { expression, .. } => self.eval_expr(expression),
Expr::Block { statements, .. } => self.evaluate_block(statements),
Expr::If {
condition,
then_branch,
elif_branches,
else_branch,
} => self.evaluate_if(condition, then_branch, elif_branches, else_branch),
Expr::While { condition, body } => {
let mut ret = BaseValue::Nil;
while self.eval_expr(condition)?.is_truthy() {
match self.eval_expr(body) {
Ok(val) => ret = val,
Err(LoxError::Return { value, .. }) => {
ret = value;
break;
}
Err(err) => return Err(err),
};
}
Ok(ret)
}
Expr::For {
variable,
condition,
increment,
body,
} => {
let source_slice = variable.source_slice.clone();
let val = self.eval_expr(variable)?;
if !matches!(val, BaseValue::Number(..)) {
return runtime_error(source_slice, "Expected number literal");
}
let mut ret = BaseValue::Nil;
while self.eval_expr(condition)?.is_truthy() {
match self.eval_expr(body) {
Ok(val) => ret = val,
Err(LoxError::Return { value, .. }) => {
ret = value;
break;
}
Err(err) => return Err(err),
};
self.eval_expr(increment)?;
}
Ok(ret)
}
}; };
// Give location-less runtime errors this node's span. // Give location-less runtime errors this node's span.
match result { match result {
@@ -125,11 +177,7 @@ impl Interpreter {
} }
} }
fn evaluate_call( fn evaluate_call(&mut self, callee: &AstNode, arguments: &[AstNode]) -> LoxResult<BaseValue> {
&mut self,
callee: &AstNode<Expr>,
arguments: &[AstNode<Expr>],
) -> LoxResult<BaseValue> {
let source_slice = callee.source_slice.clone(); let source_slice = callee.source_slice.clone();
// A bare identifier callee resolves through `locals`; anything else is // A bare identifier callee resolves through `locals`; anything else is
@@ -167,7 +215,7 @@ impl Interpreter {
} }
// Execute the function body // Execute the function body
let result = match self.evaluate(func.body) { let result = match self.eval_expr(&func.body) {
Ok(value) => Ok(value), Ok(value) => Ok(value),
Err(LoxError::Return { value, .. }) => Ok(value), Err(LoxError::Return { value, .. }) => Ok(value),
Err(err) => Err(err), Err(err) => Err(err),
@@ -228,94 +276,22 @@ impl Interpreter {
} }
} }
fn interpret_stmt_inner(&mut self, stmt: Stmt) -> LoxResult<BaseValue> {
match stmt {
Stmt::Expression { expression, .. } => self.eval_expr(&expression),
Stmt::Print { expression, .. } => {
let value = self.eval_expr(&expression)?;
println!("{}", value);
Ok(BaseValue::Nil)
}
Stmt::Block { statements, .. } => self.evaluate_block(*statements),
Stmt::Return { expression, .. } => self.eval_expr(&expression),
Stmt::VarDeclaration {
name, initializer, ..
} => {
let value = match initializer {
Some(expr_node) => self.eval_expr(&expr_node)?,
None => BaseValue::Nil,
};
self.enviorment.declare(name.clone(), value)
}
Stmt::If {
condition,
then_branch,
elif_branch,
else_branch,
..
} => self.evaluate_if(condition, then_branch, elif_branch, else_branch),
Stmt::While {
condition, body, ..
} => {
let mut ret = BaseValue::Nil;
while self.eval_expr(&condition)?.is_truthy() {
match self.evaluate(*body.clone()) {
Ok(val) => ret = val,
Err(LoxError::Return { value, .. }) => {
ret = value;
break;
}
Err(err) => return Err(err),
};
}
Ok(ret)
}
Stmt::For {
variable,
condition,
increment,
body,
..
} => {
let source_slice = variable.source_slice.clone();
let val = self.evaluate(*variable)?;
if !matches!(val, BaseValue::Number(..)) {
return runtime_error(source_slice, "Expected number literal");
}
let mut ret = BaseValue::Nil;
while self.eval_expr(&condition)?.is_truthy() {
match self.evaluate(*body.clone()) {
Ok(val) => ret = val,
Err(LoxError::Return { value, .. }) => {
ret = value;
break;
}
Err(err) => return Err(err),
};
self.evaluate(*increment.clone())?;
}
Ok(ret)
}
}
}
fn evaluate_if( fn evaluate_if(
&mut self, &mut self,
condition: Box<AstNode<Expr>>, condition: &AstNode,
then_branch: Box<AstNode<Stmt>>, then_branch: &AstNode,
elif_branch: Vec<(Box<AstNode<Expr>>, Box<AstNode<Stmt>>)>, elif_branch: &[(Box<AstNode>, Box<AstNode>)],
else_branch: Option<Box<AstNode<Stmt>>>, else_branch: &Option<Box<AstNode>>,
) -> LoxResult<BaseValue> { ) -> LoxResult<BaseValue> {
let condition = self.eval_expr(&condition)?; let condition = self.eval_expr(condition)?;
match condition { match condition {
BaseValue::Boolean(true) => self.evaluate(*then_branch), BaseValue::Boolean(true) => self.eval_expr(then_branch),
BaseValue::Boolean(false) => { BaseValue::Boolean(false) => {
for (elif_condition, elif_then_branch) in elif_branch { for (elif_condition, elif_then_branch) in elif_branch {
let condition = self.eval_expr(&elif_condition)?; let condition = self.eval_expr(elif_condition)?;
match condition { match condition {
BaseValue::Boolean(true) => { BaseValue::Boolean(true) => {
return self.evaluate(*elif_then_branch); return self.eval_expr(elif_then_branch);
} }
BaseValue::Boolean(false) => continue, BaseValue::Boolean(false) => continue,
_ => { _ => {
@@ -328,7 +304,7 @@ impl Interpreter {
}; };
} }
if let Some(else_block) = else_branch { if let Some(else_block) = else_branch {
self.evaluate(*else_block) self.eval_expr(else_block)
} else { } else {
Ok(BaseValue::Nil) Ok(BaseValue::Nil)
} }
@@ -341,25 +317,23 @@ impl Interpreter {
} }
} }
fn evaluate_block(&mut self, statements: Vec<AstNode<Stmt>>) -> LoxResult<BaseValue> { fn evaluate_block(&mut self, statements: &[AstNode]) -> LoxResult<BaseValue> {
self.enviorment.push_new_scope(); self.enviorment.push_new_scope();
// Ora elements è sempre disponibile
let mut result = Ok(BaseValue::Nil); let mut result = Ok(BaseValue::Nil);
for statement in statements.iter() { for statement in statements.iter() {
let node = statement.node.clone(); match &statement.node {
match node { Expr::Return { expression, .. } => {
Stmt::Return { expression, .. } => { let value = self.eval_expr(expression)?;
let value = self.eval_expr(&expression)?;
result = Err(LoxError::Return { result = Err(LoxError::Return {
source_slice: statement.source_slice.clone(), source_slice: statement.source_slice.clone(),
value: value, value,
return_label: "Hi".to_string(), return_label: "Hi".to_string(),
}); });
break; break;
} }
_ => result = self.evaluate((*statement).clone()), _ => result = self.eval_expr(statement),
}; };
} }
self.enviorment.pop_scope(); self.enviorment.pop_scope();
@@ -482,7 +456,7 @@ mod tests {
#[test] #[test]
fn defines_and_calls_a_function() { fn defines_and_calls_a_function() {
let src = "add :: fn (a, b) do return a + b; end add(2, 3);"; let src = "add :: fn (a, b): Number do return a + b; end; add(2, 3);";
assert_eq!(eval(src).unwrap().to_string(), "5"); assert_eq!(eval(src).unwrap().to_string(), "5");
} }
+158 -302
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
common::base_value::BaseValue, common::{base_value::BaseValue, types::Type},
frontend::{source_registry::SourceSlice, tokens::TokenType}, frontend::{source_registry::SourceSlice, tokens::TokenType},
}; };
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
@@ -22,65 +22,66 @@ impl NodeId {
NodeId(NEXT_NODE_ID.fetch_add(1, Ordering::Relaxed)) NodeId(NEXT_NODE_ID.fetch_add(1, Ordering::Relaxed))
} }
} }
/*
* grammar:
* program -> statement* EOF
* statement -> expression_statement
* | print_statement
* | var_statement
* | block_statement
* | if_statement
*
* expression_statement -> expression ";"
* print_statement -> "print" expression ";"
* var_statement -> ("var"|"dyn"|"mut")? IDENTIFIER (":" IDENTIFIER)? (("=" expression)? ";")| function_declaration
* function_declaration -> "::" "(" parameters? ")" ("->" IDENTIFIER)? block_statement
* parameters -> ( IDENTIFIER (":" IDENTIFIER)? ("," IDENTIFIER (":" IDENTIFIER)? )* )
* block_statement -> "do" statement* "end"
* if_statement -> "if" expression "then" statement ("elif" expression "then" statement)* ("else" statement)? "end"
* while_statement -> "while" expression "do" statement "end"
*
* expression -> assignment
* assignment -> IDENTIFIER "=" assignment | logical_or
* logical_or -> logical_and (("or" logical_and)*
* logical_and -> logical_is (("and" logical_is)*
* logical_is -> equality (("is" equality)*
* equality -> comparison (("==" | "!=") comparison)*
* comparison -> term ((">" | ">=" | "<" | "<=") term)*
* term -> factor (("+" | "-") factor)*
* factor -> unary (("*" | "/") unary)*
* unary -> ("!" | "-") unary | call
* call -> primary ("(" arguments ")")*
* arguments -> expression ("," expression)*
* primary -> NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" | IDENTIFIER
*/
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub enum Expr { pub enum Expr {
Literal { Literal {
value: BaseValue, value: Box<BaseValue>,
}, },
Binary { Binary {
left: Box<AstNode<Expr>>, left: Box<AstNode>,
operator: TokenType, operator: TokenType,
right: Box<AstNode<Expr>>, right: Box<AstNode>,
}, },
Unary { Unary {
operator: TokenType, operator: TokenType,
operand: Box<AstNode<Expr>>, operand: Box<AstNode>,
}, },
Grouping { Grouping {
expression: Box<AstNode<Expr>>, expression: Box<AstNode>,
}, },
Identifier { Identifier {
name: String, name: String,
}, },
Call { Call {
callee: Box<AstNode<Expr>>, callee: Box<AstNode>,
arguments: Vec<AstNode<Expr>>, arguments: Vec<AstNode>,
}, },
Assign { Assign {
name: String, name: String,
value: Box<AstNode<Expr>>, value: Box<AstNode>,
},
Print {
expression: Box<AstNode>,
},
VarDeclaration {
name: String,
var_type: Type,
initializer: Option<Box<AstNode>>,
},
Return {
expression: Box<AstNode>,
label: String,
},
Block {
statements: Box<Vec<AstNode>>,
label: String,
},
If {
condition: Box<AstNode>,
then_branch: Box<AstNode>,
elif_branches: Vec<(Box<AstNode>, Box<AstNode>)>,
else_branch: Option<Box<AstNode>>,
},
While {
condition: Box<AstNode>,
body: Box<AstNode>,
},
For {
variable: Box<AstNode>,
condition: Box<AstNode>,
increment: Box<AstNode>,
body: Box<AstNode>,
}, },
} }
@@ -95,8 +96,10 @@ impl std::fmt::Display for Expr {
right, right,
} => write!(f, "Binary ({} {} {})", left.node, operator, right.node), } => write!(f, "Binary ({} {} {})", left.node, operator, right.node),
Expr::Unary { operator, operand } => write!(f, "Unary ({} {})", operator, operand.node), Expr::Unary { operator, operand } => write!(f, "Unary ({} {})", operator, operand.node),
Expr::Grouping { expression } => write!(f, "Grouping (group {})", expression.node), Expr::Grouping { expression } => write!(f, "Grouping (group {})", expression.node,),
Expr::Identifier { name } => write!(f, "Identifier (variable {})", name), Expr::Identifier { name } => {
write!(f, "Identifier (variable {})", name)
}
Expr::Call { callee, arguments } => write!( Expr::Call { callee, arguments } => write!(
f, f,
"Call ({}({}))", "Call ({}({}))",
@@ -108,6 +111,59 @@ impl std::fmt::Display for Expr {
.join(", ") .join(", ")
), ),
Expr::Assign { name, value } => write!(f, "Assign ({} = {})", name, value.node), Expr::Assign { name, value } => write!(f, "Assign ({} = {})", name, value.node),
Expr::If {
condition,
then_branch,
elif_branches,
else_branch,
} => {
let mut result = format!("IF ({}) {{\n{}\n}}", condition.node, then_branch.node);
for (condition, branch) in elif_branches {
result.push_str(&format!(
" ELIF ({}) {{\n{}\n}}",
condition.node, branch.node,
));
}
if let Some(else_branch) = else_branch {
result.push_str(&format!(" ELSE {{\n{}\n}}", else_branch.node));
}
write!(f, "IfStmt {}", result)
}
Expr::Print { expression } => write!(f, "Print({})", expression.node),
Expr::VarDeclaration {
name,
initializer,
var_type,
} => match initializer {
Some(init) => write!(f, "Var({} : {:?} = {})", name, var_type, init.node),
None => write!(f, "Var({} : {:?})", name, var_type),
},
Expr::Return { expression, label } => {
write!(f, "Return({}, {})", expression.node, label)
}
Expr::Block { statements, label } => write!(
f,
"Block [{}] ([\n{}\n])",
label,
statements
.iter()
.map(|stmt| format!("\t \t{}", stmt.node))
.collect::<Vec<_>>()
.join("\n"),
),
Expr::While { condition, body } => {
write!(f, "While({}) {{\n{}\n}}", condition.node, body.node)
}
Expr::For {
variable,
condition,
increment,
body,
} => write!(
f,
"For({} = {} in {}) {{\n{}\n}}",
variable.node, condition.node, increment.node, body.node
),
} }
} }
} }
@@ -115,15 +171,21 @@ impl std::fmt::Display for Expr {
impl Debug for Expr { impl Debug for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Expr::Literal { value } => write!(f, "{:?}", value), Expr::Literal { value } => write!(f, "{:?} ", value,),
Expr::Binary { Expr::Binary {
left, left,
operator, operator,
right, right,
} => write!(f, "({:?} {:?} {:?})", operator, left.node, right.node), } => write!(f, "({:?} {:?} {:?}) ", operator, left.node, right.node,),
Expr::Unary { operator, operand } => write!(f, "({:?} {:?})", operator, operand.node), Expr::Unary { operator, operand } => {
Expr::Grouping { expression } => write!(f, "(group {:?})", expression.node), write!(f, "(Unary {:?} {:?}) ", operator, operand.node,)
Expr::Identifier { name } => write!(f, "(variable {:?})", name), }
Expr::Grouping { expression } => {
write!(f, "(Grouping {:?})", expression.node,)
}
Expr::Identifier { name } => {
write!(f, "(variable {:?})", name,)
}
Expr::Call { callee, arguments } => write!( Expr::Call { callee, arguments } => write!(
f, f,
"Call ({}({}))", "Call ({}({}))",
@@ -132,306 +194,90 @@ impl Debug for Expr {
.iter() .iter()
.map(|arg| arg.node.to_string()) .map(|arg| arg.node.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", ") .join(", "),
), ),
Expr::Assign { name, value } => write!(f, "(assign {:?} {:?})", name, value.node), Expr::Assign { name, value } => {
} write!(f, "(assign {:?} {:?})", name, value.node,)
} }
} Expr::If {
#[derive(Clone, PartialEq)]
pub enum Stmt {
Expression {
expression: Box<AstNode<Expr>>,
return_value: Box<BaseValue>,
},
Print {
expression: Box<AstNode<Expr>>,
return_value: Box<BaseValue>,
},
VarDeclaration {
name: String,
initializer: Option<Box<AstNode<Expr>>>,
return_value: Box<BaseValue>,
},
Return {
expression: Box<AstNode<Expr>>,
label: String,
return_value: Box<BaseValue>,
},
Block {
statements: Box<Vec<AstNode<Stmt>>>,
label: String,
return_value: Box<BaseValue>,
},
If {
condition: Box<AstNode<Expr>>,
then_branch: Box<AstNode<Stmt>>,
elif_branch: Vec<(Box<AstNode<Expr>>, Box<AstNode<Stmt>>)>,
else_branch: Option<Box<AstNode<Stmt>>>,
return_value: Box<BaseValue>,
},
While {
condition: Box<AstNode<Expr>>,
body: Box<AstNode<Stmt>>,
return_value: Box<BaseValue>,
},
For {
variable: Box<AstNode<Stmt>>,
condition: Box<AstNode<Expr>>,
increment: Box<AstNode<Stmt>>,
body: Box<AstNode<Stmt>>,
return_value: Box<BaseValue>,
},
}
impl Display for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Stmt::Expression {
expression,
return_value,
} => write!(f, "Expression ({}) -> {:?}", expression.node, return_value),
Stmt::If {
condition, condition,
then_branch, then_branch,
elif_branch, elif_branches,
else_branch, else_branch,
return_value,
} => { } => {
let mut result = format!("IF ({}) {{\n{}\n}}", condition.node, then_branch.node); let mut result =
for (condition, branch) in elif_branch { format!("IF ({:?}) {{\n{:?}\n}}", condition.node, then_branch.node,);
for (condition, branch) in elif_branches {
result.push_str(&format!( result.push_str(&format!(
" ELIF ({}) {{\n{}\n}} -> {:?}", " ELIF ({:?}) {{\n{:?}\n}}",
condition.node, branch.node, return_value condition.node, branch.node,
)); ));
} }
if let Some(else_branch) = else_branch { if let Some(else_branch) = else_branch {
result.push_str(&format!( result.push_str(&format!(" ELSE {{\n{:?}\n}}", else_branch.node,));
" ELSE {{\n{}\n}} -> {:?}",
else_branch.node, return_value
));
}
write!(f, "IfStmt {}", result)
}
Stmt::Print {
expression,
return_value,
} => write!(f, "Print({}) -> {:?};", expression.node, return_value),
Stmt::VarDeclaration {
name,
initializer,
return_value,
} => match initializer {
Some(init) => write!(f, "Var({} = {}) -> {:?};", name, init.node, return_value),
None => write!(f, "Var({}) -> {:?};", name, return_value),
},
Stmt::Return {
expression,
return_value,
label,
} => write!(
f,
"Return({}, {}) -> {:?};",
expression.node, label, return_value
),
Stmt::Block {
statements,
label,
return_value,
} => write!(
f,
"Block [{}] ([\n{}\n]) -> {:?}",
label,
statements
.iter()
.map(|stmt| format!("\t \t{}", stmt.node))
.collect::<Vec<_>>()
.join("\n"),
return_value
),
Stmt::While {
condition,
body,
return_value,
} => {
write!(
f,
"While({}) {{\n{}\n}} -> {:?}",
condition.node, body.node, return_value
)
}
Stmt::For {
variable,
condition,
increment,
body,
return_value,
} => write!(
f,
"For({} = {} in {}) {{\n{}\n}} -> {:?}",
variable.node, condition.node, increment.node, body.node, return_value
),
}
}
}
impl Debug for Stmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Stmt::Expression {
expression,
return_value,
} => write!(
f,
" Expression ({:?}) -> {:?}",
expression.node, return_value
),
Stmt::If {
condition,
then_branch,
elif_branch,
else_branch,
return_value,
} => {
let mut result = format!(
"IF ({:?}) {{\n{:?}\n}} -> {:?} ",
condition.node, then_branch.node, return_value
);
for (condition, branch) in elif_branch {
result.push_str(&format!(
" ELIF ({:?}) {{\n{:?}\n}} -> {:?} ",
condition.node, branch.node, return_value
));
}
if let Some(else_branch) = else_branch {
result.push_str(&format!(
" ELSE {{\n{:?}\n}} -> {:?}",
else_branch.node, return_value
));
} }
write!(f, "IfStmt {:?}", result) write!(f, "IfStmt {:?}", result)
} }
Stmt::Print { Expr::Print { expression } => {
expression, write!(f, "Print({:?});", expression.node,)
return_value, }
} => write!(f, "Print({:?}) -> {:?};", expression.node, return_value), Expr::VarDeclaration {
Stmt::VarDeclaration {
name, name,
initializer, initializer,
return_value, var_type,
} => match initializer { } => match initializer {
Some(init) => write!( Some(init) => write!(f, "Var({:?}: {:?} = {:?});", name, var_type, init.node,),
f, None => write!(f, "Var({:?}: {:?});", name, var_type,),
"Var({:?} = {:?}) -> {:?};",
name, init.node, return_value
),
None => write!(f, "Var({:?}) -> {:?};", name, return_value),
}, },
Stmt::Return { Expr::Return { expression, label } => {
expression, write!(f, "Return({:?}, {}) ;", expression.node, label,)
return_value, }
label, Expr::Block { label, statements } => write!(
} => write!(
f, f,
"Return({:?}, {}) -> {:?};", "Block [{}] ([\n{:?}\n])",
expression.node, label, return_value
),
Stmt::Block {
label,
statements,
return_value,
} => write!(
f,
"Block [{}] ([\n{:?}\n]) -> {:?}",
label, label,
statements statements
.iter() .iter()
.map(|stmt| format!("{:?}", stmt.node)) .map(|stmt| format!("{:?}", stmt.node))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\t\t\n"), .join("\t\t\n"),
return_value
), ),
Stmt::While { Expr::While { condition, body } => {
condition, write!(f, "While({:?}) {{\n{:?}\n}}", condition.node, body.node,)
body,
return_value,
} => {
write!(
f,
"While({:?}) {{\n{:?}\n}} -> {:?}",
condition.node, body.node, return_value
)
} }
Stmt::For { Expr::For {
variable, variable,
condition, condition,
increment, increment,
body, body,
return_value,
} => write!( } => write!(
f, f,
"For({:?} = {:?} in {:?}) {{\n{:?}\n}} -> {:?}", "For({:?} = {:?} in {:?}) {{\n{:?}\n}} ",
variable.node, condition.node, increment.node, body.node, return_value variable.node, condition.node, increment.node, body.node,
), ),
} }
} }
} }
pub trait AstNodeKind {
fn kind(&self) -> &'static str;
}
impl AstNodeKind for Expr {
fn kind(&self) -> &'static str {
match self {
Expr::Literal { value: _ } => "literal",
Expr::Binary {
left: _,
operator: _,
right: _,
} => "binary expression",
Expr::Unary { .. } => "unary expression",
Expr::Grouping { .. } => "grouping expression",
Expr::Identifier { .. } => "variable expression",
Expr::Call { .. } => "call expression",
Expr::Assign { .. } => "assignment expression",
}
}
}
impl AstNodeKind for Stmt {
fn kind(&self) -> &'static str {
match self {
Stmt::Expression { .. } => "expression",
Stmt::VarDeclaration { .. } => "variable declaration",
Stmt::Return { .. } => "return statement",
Stmt::Block { .. } => "block statement",
Stmt::If { .. } => "if statement",
Stmt::Print { .. } => "print statement",
Stmt::While { .. } => "while statement",
Stmt::For { .. } => "for statement",
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct AstNode<T: AstNodeKind + Debug + Display> { pub struct AstNode {
/// Stable identity, assigned at construction and preserved across clones. /// Stable identity, assigned at construction and preserved across clones.
pub id: NodeId, pub id: NodeId,
pub node: T, pub node: Expr,
pub is_statement: bool,
pub source_slice: SourceSlice, pub source_slice: SourceSlice,
pub return_type: Type,
} }
// Identity (`id`) deliberately does not participate in equality: two nodes are // Identity (`id`) deliberately does not participate in equality: two nodes are
// equal when their content and location match, regardless of node id. // equal when their content and location match, regardless of node id.
impl<T: AstNodeKind + Debug + Display + PartialEq> PartialEq for AstNode<T> { impl PartialEq for AstNode {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.node == other.node && self.source_slice == other.source_slice self.node == other.node && self.source_slice == other.source_slice
} }
} }
impl<T: AstNodeKind + Debug + Display> Display for AstNode<T> { impl Display for AstNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
@@ -441,7 +287,7 @@ impl<T: AstNodeKind + Debug + Display> Display for AstNode<T> {
} }
} }
impl<T: AstNodeKind + Debug + Display> Debug for AstNode<T> { impl Debug for AstNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
@@ -451,12 +297,22 @@ impl<T: AstNodeKind + Debug + Display> Debug for AstNode<T> {
} }
} }
impl<T: AstNodeKind + Debug + Display> AstNode<T> { impl AstNode {
pub fn new(node: T, source_slice: SourceSlice) -> Self { fn new(node: Expr, source_slice: SourceSlice, type_name: String, is_statement: bool) -> Self {
AstNode { AstNode {
id: NodeId::next(), id: NodeId::next(),
node, node,
is_statement,
source_slice, source_slice,
return_type: Type::Unresolved(type_name),
} }
} }
pub fn new_statement(node: Expr, source_slice: SourceSlice) -> Self {
Self::new(node, source_slice, "Nil".to_string(), true)
}
pub fn new_expression(node: Expr, source_slice: SourceSlice, type_name: String) -> Self {
Self::new(node, source_slice, type_name, false)
}
} }
+28 -17
View File
@@ -1,12 +1,12 @@
use core::fmt; use core::fmt;
use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::ops::{Add, Div, Mul, Not, Rem, Sub}; use std::ops::{Add, Div, Mul, Not, Rem, Sub};
use crate::backend::environment::Environment;
use crate::common::lox_result::{runtime_error, LoxResult}; use crate::common::lox_result::{runtime_error, LoxResult};
use crate::common::types::Type;
use crate::{ use crate::{
backend::environment::EnvironmentStack, backend::environment::EnvironmentStack, common::ast::AstNode,
common::ast::{AstNode, Expr, Stmt},
frontend::source_registry::SourceSlice, frontend::source_registry::SourceSlice,
}; };
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@@ -272,15 +272,20 @@ pub enum BaseValue {
Nil, Nil,
Function(LoxFunction), Function(LoxFunction),
NativeFunction(NativeFunction), NativeFunction(NativeFunction),
Struct(Struct),
}
struct Dict {
map: HashMap<String, BaseValue>,
} }
struct ReturnValue { struct ReturnValue {
label: Vec<String>, label: Vec<String>,
value: BaseValue, value: Type,
} }
impl ReturnValue { impl ReturnValue {
pub fn new(label: Vec<String>, value: BaseValue) -> Self { pub fn new(label: Vec<String>, value: Type) -> Self {
Self { label, value } Self { label, value }
} }
} }
@@ -304,39 +309,40 @@ impl Display for BaseValue {
BaseValue::Nil => write!(f, "nil"), BaseValue::Nil => write!(f, "nil"),
BaseValue::Function(..) => write!(f, "<fn lox function>"), BaseValue::Function(..) => write!(f, "<fn lox function>"),
BaseValue::NativeFunction(..) => write!(f, "<native native function>"), BaseValue::NativeFunction(..) => write!(f, "<native native function>"),
BaseValue::Struct(_) => write!(f, "<struct>"),
} }
} }
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct LoxFunction { pub struct LoxFunction {
pub parameters: Vec<(String, String)>, pub parameters: Vec<(String, Type)>,
pub return_type: Option<String>, pub return_type: Type,
pub body: AstNode<Stmt>, pub body: AstNode,
pub closure: Option<EnvironmentStack<BaseValue>>, pub closure: Option<EnvironmentStack<BaseValue>>,
pub guard: Option<Box<AstNode<Expr>>>, pub guard: Option<Box<AstNode>>,
} }
impl LoxFunction { impl LoxFunction {
pub fn new( pub fn new(
parameters: Vec<(String, String)>, parameters: Vec<(String, Type)>,
body: AstNode<Stmt>, body: AstNode,
closure: Option<EnvironmentStack<BaseValue>>, closure: Option<EnvironmentStack<BaseValue>>,
guard: Option<Box<AstNode<Expr>>>, guard: Option<Box<AstNode>>,
) -> Self { ) -> Self {
LoxFunction { LoxFunction {
parameters, parameters,
return_type: None, return_type: Type::Any,
body, body,
closure, closure,
guard, guard,
} }
} }
pub fn anonymous_function(parameters: Vec<(String, String)>, body: AstNode<Stmt>) -> Self { pub fn anonymous_function(parameters: Vec<(String, Type)>, body: AstNode) -> Self {
LoxFunction { LoxFunction {
parameters, parameters,
return_type: None, return_type: Type::Any,
body, body,
closure: None, closure: None,
guard: None, guard: None,
@@ -346,12 +352,12 @@ impl LoxFunction {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct NativeFunction { pub struct NativeFunction {
pub parameters: Vec<(String, String)>, pub parameters: Vec<(String, Type)>,
pub function: fn(&[BaseValue]) -> BaseValue, pub function: fn(&[BaseValue]) -> BaseValue,
} }
impl NativeFunction { impl NativeFunction {
pub fn new(parameters: Vec<(String, String)>, function: fn(&[BaseValue]) -> BaseValue) -> Self { pub fn new(parameters: Vec<(String, Type)>, function: fn(&[BaseValue]) -> BaseValue) -> Self {
NativeFunction { NativeFunction {
parameters, parameters,
function, function,
@@ -359,6 +365,11 @@ impl NativeFunction {
} }
} }
#[derive(Debug, Clone, PartialEq)]
pub struct Struct {
pub fields: Vec<(String, BaseValue)>,
}
// Trait implementations for BaseValue // Trait implementations for BaseValue
pub trait Truthy { pub trait Truthy {
+1
View File
@@ -1,3 +1,4 @@
pub mod ast; pub mod ast;
pub mod base_value; pub mod base_value;
pub mod lox_result; pub mod lox_result;
pub mod types;
+70
View File
@@ -0,0 +1,70 @@
use std::{fmt::Display, rc::Rc};
#[derive(Debug, Clone, PartialEq)]
pub enum Type {
Any,
Number,
String,
Boolean,
Nil,
Function(FunctionType),
Struct(Rc<StructType>),
Unresolved(String),
}
impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Any => write!(f, "any"),
Type::Number => write!(f, "number"),
Type::String => write!(f, "string"),
Type::Boolean => write!(f, "boolean"),
Type::Nil => write!(f, "nil"),
Type::Function(fun) => write!(f, "{}", fun),
Type::Struct(struct_) => write!(f, "{}", struct_),
Type::Unresolved(name) => write!(f, "{}", name),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionType {
pub params: Vec<(String, Box<Type>)>,
pub return_type: Box<Type>,
}
impl Display for FunctionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let rendered_params = self
.params
.iter()
.map(|(name, ty)| format!("{}: {}", name, ty))
.collect::<Vec<_>>()
.join(", ");
write!(f, "fn({}) -> {}", rendered_params, self.return_type)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StructType {
pub fields: Vec<(String, Box<Type>)>,
pub methods: Vec<(String, FunctionType)>,
}
impl Display for StructType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let rendered_field = self
.fields
.iter()
.map(|(name, ty)| format!("{}: {}", name, ty))
.collect::<Vec<_>>()
.join(", ");
let rendered_method = self
.methods
.iter()
.map(|(name, ty)| format!("{}: {}", name, ty))
.collect::<Vec<_>>()
.join(", ");
write!(f, "struct {{ {}, {} }}", rendered_field, rendered_method)
}
}
+29 -1
View File
@@ -16,7 +16,7 @@ pub struct Lexer {
fn get_keyword_token(word: &str) -> Option<TokenType> { fn get_keyword_token(word: &str) -> Option<TokenType> {
match word { match word {
"and" => Some(TokenType::And), "and" => Some(TokenType::And),
"class" => Some(TokenType::Class), "struct" => Some(TokenType::Struct),
"do" => Some(TokenType::StartBlock), "do" => Some(TokenType::StartBlock),
"end" => Some(TokenType::EndBlock), "end" => Some(TokenType::EndBlock),
"false" => Some(TokenType::False), "false" => Some(TokenType::False),
@@ -514,4 +514,32 @@ mod tests {
assert_eq!(tokens[1].source_slice.start_position.line, 1); assert_eq!(tokens[1].source_slice.start_position.line, 1);
assert_eq!(tokens[1].source_slice.start_position.column, 0); assert_eq!(tokens[1].source_slice.start_position.column, 0);
} }
#[test]
fn handle_struct() {
let tokens = lex("Cosa :: struct {field1: Number, field2: String}");
let tokens_tyepe: Vec<TokenType> = tokens.iter().map(|x| x.token_type.clone()).collect();
let tokent_for_check = vec![
TokenType::Identifier,
TokenType::Colon,
TokenType::Colon,
TokenType::Struct,
TokenType::LeftBrace,
TokenType::Identifier,
TokenType::Colon,
TokenType::Identifier,
TokenType::Comma,
TokenType::Identifier,
TokenType::Colon,
TokenType::Identifier,
TokenType::RightBrace,
TokenType::Eof,
];
assert_eq!(tokens_tyepe, tokent_for_check);
assert_eq!(tokens[0].lexeme, "Cosa");
assert_eq!(tokens[5].lexeme, "field1");
assert_eq!(tokens[7].lexeme, "Number");
assert_eq!(tokens[9].lexeme, "field2");
assert_eq!(tokens[11].lexeme, "String");
}
} }
+266 -268
View File
File diff suppressed because it is too large Load Diff
+8
View File
@@ -121,6 +121,14 @@ impl SourceSlice {
end_position, end_position,
} }
} }
pub fn from_source_slices(start_point: SourceSlice, end_point: SourceSlice) -> Self {
Self {
source_id: start_point.source_id,
start_position: start_point.start_position,
end_position: end_point.end_position,
}
}
pub fn tree_point( pub fn tree_point(
source_id: SourceId, source_id: SourceId,
column_start: usize, column_start: usize,
+4 -2
View File
@@ -35,11 +35,12 @@ pub enum TokenType {
Identifier, Identifier,
String, String,
Number, Number,
Atom,
// Keywords // Keywords
Fn, Fn,
And, And,
Class, Struct,
StartBlock, StartBlock,
EndBlock, EndBlock,
False, False,
@@ -80,8 +81,9 @@ impl fmt::Display for TokenType {
TokenType::Identifier => write!(f, "IDENTIFIER"), TokenType::Identifier => write!(f, "IDENTIFIER"),
TokenType::String => write!(f, "STRING"), TokenType::String => write!(f, "STRING"),
TokenType::Number => write!(f, "NUMBER"), TokenType::Number => write!(f, "NUMBER"),
TokenType::Atom => write!(f, "ATOM"),
TokenType::And => write!(f, "and"), TokenType::And => write!(f, "and"),
TokenType::Class => write!(f, "class"), TokenType::Struct => write!(f, "struct"),
TokenType::Else => write!(f, "else"), TokenType::Else => write!(f, "else"),
TokenType::False => write!(f, "false"), TokenType::False => write!(f, "false"),
TokenType::True => write!(f, "true"), TokenType::True => write!(f, "true"),
+1
View File
@@ -1,4 +1,5 @@
pub mod backend; pub mod backend;
pub mod common; pub mod common;
pub mod frontend; pub mod frontend;
pub mod logging;
pub mod middleend; pub mod middleend;
+16 -67
View File
@@ -5,7 +5,7 @@
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use crate::common::ast::{AstNode, AstNodeKind, Expr, Stmt}; use crate::common::ast::{AstNode, Expr};
/// Configuration for pretty printing output /// Configuration for pretty printing output
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@@ -264,33 +264,7 @@ impl PrettyPrint for Expr {
write!(f, "] }}") write!(f, "] }}")
} }
} }
} Expr::Print { expression } => {
}
}
impl PrettyPrint for Stmt {
fn pretty_print(&self, ctx: &PrettyContext, f: &mut fmt::Formatter) -> fmt::Result {
if ctx.should_truncate() {
return write!(f, "{}...", ctx.indent());
}
let indent = ctx.indent();
match self {
Stmt::Expression { expression, .. } => {
if ctx.config.compact {
let expr_str =
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
write!(f, "{};", expr_str)
} else {
writeln!(f, "{}Expression {{", indent)?;
write!(f, "{}expression: ", ctx.child_context(true).indent())?;
expression.pretty_print(&ctx.child_context(true), f)?;
writeln!(f)?;
write!(f, "{}}}", indent)
}
}
Stmt::Print { expression, .. } => {
if ctx.config.compact { if ctx.config.compact {
let expr_str = let expr_str =
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact()); pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
@@ -303,7 +277,7 @@ impl PrettyPrint for Stmt {
write!(f, "{}}}", indent) write!(f, "{}}}", indent)
} }
} }
Stmt::VarDeclaration { Expr::VarDeclaration {
name, initializer, .. name, initializer, ..
} => { } => {
if ctx.config.compact { if ctx.config.compact {
@@ -331,8 +305,7 @@ impl PrettyPrint for Stmt {
write!(f, "{}}}", indent) write!(f, "{}}}", indent)
} }
} }
Expr::Return { expression, .. } => {
Stmt::Return { expression, .. } => {
if ctx.config.compact { if ctx.config.compact {
let expr_str = let expr_str =
pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact()); pretty_print_with_config(expression.as_ref(), &PrettyConfig::compact());
@@ -345,9 +318,7 @@ impl PrettyPrint for Stmt {
write!(f, "{}}}", indent) write!(f, "{}}}", indent)
} }
} }
Stmt::Block { Expr::Block { statements, label } => {
statements, label, ..
} => {
if ctx.config.compact { if ctx.config.compact {
write!(f, "{{ ... [{}] ({} statements) }}", label, statements.len()) write!(f, "{{ ... [{}] ({} statements) }}", label, statements.len())
} else { } else {
@@ -366,12 +337,11 @@ impl PrettyPrint for Stmt {
write!(f, "{}}}", indent) write!(f, "{}}}", indent)
} }
} }
Stmt::If { Expr::If {
condition, condition,
then_branch, then_branch,
elif_branch, elif_branches,
else_branch, else_branch,
..
} => { } => {
if ctx.config.compact { if ctx.config.compact {
let cond_str = let cond_str =
@@ -386,10 +356,10 @@ impl PrettyPrint for Stmt {
then_branch.pretty_print(&ctx.child_context(false), f)?; then_branch.pretty_print(&ctx.child_context(false), f)?;
writeln!(f, ",")?; writeln!(f, ",")?;
if !elif_branch.is_empty() { if !elif_branches.is_empty() {
writeln!(f, "{}elif_branches: [", ctx.child_context(false).indent())?; writeln!(f, "{}elif_branches: [", ctx.child_context(false).indent())?;
for (i, (cond, branch)) in elif_branch.iter().enumerate() { for (i, (cond, branch)) in elif_branches.iter().enumerate() {
let is_last = i == elif_branch.len() - 1; let is_last = i == elif_branches.len() - 1;
let child_ctx = ctx.child_context(false).child_context(is_last); let child_ctx = ctx.child_context(false).child_context(is_last);
writeln!(f, "{}(", child_ctx.indent())?; writeln!(f, "{}(", child_ctx.indent())?;
write!(f, "{}condition: ", child_ctx.child_context(false).indent())?; write!(f, "{}condition: ", child_ctx.child_context(false).indent())?;
@@ -421,21 +391,18 @@ impl PrettyPrint for Stmt {
write!(f, "{}}}", indent) write!(f, "{}}}", indent)
} }
} }
Stmt::While { Expr::While { condition, body } => {
condition, body, ..
} => {
write!(f, "{}while (", ctx.child_context(true).indent())?; write!(f, "{}while (", ctx.child_context(true).indent())?;
condition.pretty_print(&ctx.child_context(true), f)?; condition.pretty_print(&ctx.child_context(true), f)?;
writeln!(f, ") {{")?; writeln!(f, ") {{")?;
body.pretty_print(&ctx.child_context(true), f)?; body.pretty_print(&ctx.child_context(true), f)?;
writeln!(f, "{}}}", ctx.child_context(true).indent()) writeln!(f, "{}}}", ctx.child_context(true).indent())
} }
Stmt::For { Expr::For {
variable, variable,
condition, condition,
increment, increment,
body, body,
..
} => { } => {
write!(f, "{}for (", ctx.child_context(true).indent())?; write!(f, "{}for (", ctx.child_context(true).indent())?;
variable.pretty_print(&ctx.child_context(true), f)?; variable.pretty_print(&ctx.child_context(true), f)?;
@@ -451,7 +418,7 @@ impl PrettyPrint for Stmt {
} }
} }
impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrint for AstNode<T> { impl PrettyPrint for AstNode {
fn pretty_print(&self, ctx: &PrettyContext, f: &mut fmt::Formatter) -> fmt::Result { fn pretty_print(&self, ctx: &PrettyContext, f: &mut fmt::Formatter) -> fmt::Result {
let indent = ctx.indent(); let indent = ctx.indent();
@@ -463,9 +430,9 @@ impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrint for A
if ctx.config.show_types { if ctx.config.show_types {
writeln!( writeln!(
f, f,
"{}kind: {:?},", "{}type: {},",
ctx.child_context(false).indent(), ctx.child_context(false).indent(),
self.node.kind() self.return_type
)?; )?;
} }
@@ -519,25 +486,7 @@ impl PrettyPrintExt for Expr {
} }
} }
impl PrettyPrintExt for Stmt { impl PrettyPrintExt for AstNode {
fn pretty(&self) -> String {
pretty_print(self)
}
fn pretty_compact(&self) -> String {
pretty_print_with_config(self, &PrettyConfig::compact())
}
fn pretty_tree(&self) -> String {
pretty_print_with_config(self, &PrettyConfig::tree())
}
fn pretty_with_config(&self, config: &PrettyConfig) -> String {
pretty_print_with_config(self, config)
}
}
impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrintExt for AstNode<T> {
fn pretty(&self) -> String { fn pretty(&self) -> String {
pretty_print(self) pretty_print(self)
} }
+4 -2
View File
@@ -174,7 +174,8 @@ impl Token {
TokenType::String => "STR", TokenType::String => "STR",
TokenType::Number => "NUM", TokenType::Number => "NUM",
TokenType::And => "AND", TokenType::And => "AND",
TokenType::Class => "CLASS", TokenType::Struct => "STRUCT",
TokenType::Atom => "ATOM",
TokenType::StartBlock => "DO", TokenType::StartBlock => "DO",
TokenType::EndBlock => "END", TokenType::EndBlock => "END",
TokenType::False => "FALSE", TokenType::False => "FALSE",
@@ -219,7 +220,7 @@ impl Token {
| TokenType::Less | TokenType::Less
| TokenType::LessEqual => ("\x1b[31m", self.token_type_symbol()), | TokenType::LessEqual => ("\x1b[31m", self.token_type_symbol()),
TokenType::And TokenType::And
| TokenType::Class | TokenType::Struct
| TokenType::False | TokenType::False
| TokenType::True | TokenType::True
| TokenType::Fun | TokenType::Fun
@@ -239,6 +240,7 @@ impl Token {
| TokenType::Var | TokenType::Var
| TokenType::Fn | TokenType::Fn
| TokenType::Is | TokenType::Is
| TokenType::Atom
| TokenType::Val => ("\x1b[34m", self.token_type_symbol()), | TokenType::Val => ("\x1b[34m", self.token_type_symbol()),
TokenType::Identifier | TokenType::String | TokenType::Number => { TokenType::Identifier | TokenType::String | TokenType::Number => {
("\x1b[32m", self.token_type_symbol()) ("\x1b[32m", self.token_type_symbol())
+10 -11
View File
@@ -1,13 +1,12 @@
mod backend; pub mod backend;
mod common; pub mod common;
mod frontend; pub mod frontend;
mod logging; pub mod logging;
mod middleend; pub mod middleend;
use crate::{ use crate::{
backend::interpreter::{EvaluateInterpreter, Interpreter}, backend::interpreter::{EvaluateInterpreter, Interpreter},
common::{ common::{
ast::{AstNode, Stmt}, ast::AstNode,
lox_result::{LoxError, LoxResult}, lox_result::{LoxError, LoxResult},
}, },
frontend::{ frontend::{
@@ -175,7 +174,7 @@ impl LoxInterpreter {
} }
// Funzione per parsare fino all'AST // Funzione per parsare fino all'AST
fn parse_to_ast(&self, source_id: SourceId) -> LoxResult<Vec<AstNode<Stmt>>> { fn parse_to_ast(&self, source_id: SourceId) -> LoxResult<Vec<AstNode>> {
let tokens = self.tokenize(source_id)?; let tokens = self.tokenize(source_id)?;
Parser::new(tokens).parse().or_else(|err| { Parser::new(tokens).parse().or_else(|err| {
@@ -185,7 +184,7 @@ impl LoxInterpreter {
} }
// Funzione per eseguire l'AST // Funzione per eseguire l'AST
fn execute_ast(&self, ast: Vec<AstNode<Stmt>>, debug: bool) -> LoxResult<String> { fn execute_ast(&self, ast: Vec<AstNode>, debug: bool) -> LoxResult<String> {
// Static resolution pass: compute variable scope distances and report // Static resolution pass: compute variable scope distances and report
// any resolution errors before executing. // any resolution errors before executing.
let locals = self.resolve(&ast)?; let locals = self.resolve(&ast)?;
@@ -212,7 +211,7 @@ impl LoxInterpreter {
// Static variable resolution; prints and returns the first error, if any. // Static variable resolution; prints and returns the first error, if any.
fn resolve( fn resolve(
&self, &self,
ast: &[AstNode<Stmt>], ast: &[AstNode],
) -> LoxResult<std::collections::HashMap<crate::common::ast::NodeId, usize>> { ) -> LoxResult<std::collections::HashMap<crate::common::ast::NodeId, usize>> {
Resolver::resolve_program(ast).map_err(|errors| { Resolver::resolve_program(ast).map_err(|errors| {
for err in &errors { for err in &errors {
@@ -295,7 +294,7 @@ fn format_tokens(tokens: &[crate::frontend::tokens::Token]) -> String {
.join("\n") .join("\n")
} }
fn format_ast(ast: &[AstNode<Stmt>]) -> String { fn format_ast(ast: &[AstNode]) -> String {
use crate::logging::display_ast::{pretty_print_with_config, PrettyConfig}; use crate::logging::display_ast::{pretty_print_with_config, PrettyConfig};
let config = PrettyConfig { let config = PrettyConfig {
+43 -52
View File
@@ -17,13 +17,13 @@ use std::collections::HashMap;
use crate::{ use crate::{
common::{ common::{
ast::{AstNode, Expr, NodeId, Stmt}, ast::{AstNode, Expr, NodeId},
base_value::{BaseValue, LoxFunction}, base_value::{BaseValue, LoxFunction},
lox_result::{ErrorSink, LoxError, LoxResult}, lox_result::{ErrorSink, LoxError, LoxResult},
}, },
middleend::{ middleend::{
scope_stack::ScopeStack, scope_stack::ScopeStack,
visit_ast::{walk_expr, walk_function, walk_stmt, Visitor}, visit_ast::{walk_expr, walk_function, Visitor},
}, },
}; };
@@ -55,13 +55,13 @@ impl Resolver {
/// Resolve a whole program, returning the per-reference scope distances or /// Resolve a whole program, returning the per-reference scope distances or
/// the accumulated resolution errors. /// the accumulated resolution errors.
pub fn resolve_program( pub fn resolve_program(
statements: &[AstNode<Stmt>], statements: &[AstNode],
) -> Result<HashMap<NodeId, usize>, Vec<LoxError>> { ) -> Result<HashMap<NodeId, usize>, Vec<LoxError>> {
let mut resolver = Resolver::new(); let mut resolver = Resolver::new();
for statement in statements { for statement in statements {
// Visiting only fails for fatal/internal errors, which this pass // Visiting only fails for fatal/internal errors, which this pass
// never produces; user-facing diagnostics go to `errors`. // never produces; user-facing diagnostics go to `errors`.
let _ = resolver.visit_stmt(statement); let _ = resolver.visit_expr(statement);
} }
if resolver.errors.has_errors() { if resolver.errors.has_errors() {
Err(resolver.errors.into_errors()) Err(resolver.errors.into_errors())
@@ -105,52 +105,7 @@ impl Default for Resolver {
} }
impl Visitor for Resolver { impl Visitor for Resolver {
fn visit_stmt(&mut self, stmt: &AstNode<Stmt>) -> LoxResult<()> { fn visit_expr(&mut self, expr: &AstNode) -> LoxResult<()> {
match &stmt.node {
Stmt::VarDeclaration {
name, initializer, ..
} => {
// A function declaration is a var bound to a function literal.
// Define its name *before* resolving the body so it can recurse;
// a plain variable is defined *after* its initializer so that
// `var a = a;` is caught.
let is_function = matches!(
initializer.as_deref().map(|node| &node.node),
Some(Expr::Literal {
value: BaseValue::Function(_)
})
);
self.declare(name, &stmt.source_slice);
if is_function {
self.define(name);
walk_stmt(self, stmt)?;
} else {
walk_stmt(self, stmt)?; // resolves the initializer, if any
self.define(name);
}
Ok(())
}
Stmt::Block { .. } => {
self.scopes.begin_scope();
walk_stmt(self, stmt)?;
self.scopes.end_scope();
Ok(())
}
Stmt::Return { .. } => {
if self.current_function == FunctionType::None {
self.errors.report(LoxError::ParseError {
source_slice: stmt.source_slice.clone(),
message: "Can't return from top-level code.".to_string(),
});
}
walk_stmt(self, stmt) // resolve the returned expression
}
// Expression, Print, If, While, For: default traversal.
_ => walk_stmt(self, stmt),
}
}
fn visit_expr(&mut self, expr: &AstNode<Expr>) -> LoxResult<()> {
match &expr.node { match &expr.node {
Expr::Identifier { name } => { Expr::Identifier { name } => {
if self.scopes.get_local(name) == Some(&false) { if self.scopes.get_local(name) == Some(&false) {
@@ -167,6 +122,42 @@ impl Visitor for Resolver {
self.resolve_local(expr.id, name); self.resolve_local(expr.id, name);
Ok(()) Ok(())
} }
Expr::VarDeclaration {
name, initializer, ..
} => {
// A function declaration is a var bound to a function literal.
// Define its name *before* resolving the body so it can recurse;
// a plain variable is defined *after* its initializer so that
// `var a = a;` is caught.
let is_function = matches!(
initializer.as_deref().map(|node| &node.node),
Some(Expr::Literal { value }) if matches!(**value, BaseValue::Function(_))
);
self.declare(name, &expr.source_slice);
if is_function {
self.define(name);
walk_expr(self, expr)?;
} else {
walk_expr(self, expr)?; // resolves the initializer, if any
self.define(name);
}
Ok(())
}
Expr::Block { .. } => {
self.scopes.begin_scope();
walk_expr(self, expr)?;
self.scopes.end_scope();
Ok(())
}
Expr::Return { .. } => {
if self.current_function == FunctionType::None {
self.errors.report(LoxError::ParseError {
source_slice: expr.source_slice.clone(),
message: "Can't return from top-level code.".to_string(),
});
}
walk_expr(self, expr) // resolve the returned expression
}
_ => walk_expr(self, expr), _ => walk_expr(self, expr),
} }
} }
@@ -193,7 +184,7 @@ mod tests {
use crate::frontend::lexer::Lexer; use crate::frontend::lexer::Lexer;
use crate::frontend::parser::Parser; use crate::frontend::parser::Parser;
fn parse(src: &str) -> Vec<AstNode<Stmt>> { fn parse(src: &str) -> Vec<AstNode> {
let tokens = Lexer::new(src.to_string(), 0) let tokens = Lexer::new(src.to_string(), 0)
.scans_tokens() .scans_tokens()
.expect("source should lex"); .expect("source should lex");
@@ -251,7 +242,7 @@ mod tests {
#[test] #[test]
fn return_inside_a_function_is_allowed() { fn return_inside_a_function_is_allowed() {
let locals = resolve("f :: fn (n) do return n; end").expect("should resolve"); let locals = resolve("f :: fn (n): Number do return n; end;").expect("should resolve");
// `n` resolves from the body block up to the parameter scope. // `n` resolves from the body block up to the parameter scope.
assert_eq!(locals.len(), 1); assert_eq!(locals.len(), 1);
assert_eq!(*locals.values().next().unwrap(), 1); assert_eq!(*locals.values().next().unwrap(), 1);
+70 -90
View File
@@ -23,7 +23,7 @@
//! struct IdentifierCounter { count: usize } //! struct IdentifierCounter { count: usize }
//! //!
//! impl Visitor for IdentifierCounter { //! impl Visitor for IdentifierCounter {
//! fn visit_expr(&mut self, expr: &AstNode<Expr>) -> LoxResult<()> { //! fn visit_expr(&mut self, expr: &AstNode) -> LoxResult<()> {
//! if let Expr::Identifier { .. } = &expr.node { //! if let Expr::Identifier { .. } = &expr.node {
//! self.count += 1; //! self.count += 1;
//! } //! }
@@ -33,7 +33,7 @@
//! ``` //! ```
use crate::common::{ use crate::common::{
ast::{AstNode, Expr, Stmt}, ast::{AstNode, Expr},
base_value::{BaseValue, LoxFunction}, base_value::{BaseValue, LoxFunction},
lox_result::LoxResult, lox_result::LoxResult,
}; };
@@ -43,13 +43,8 @@ use crate::common::{
/// Every hook has a default implementation that performs the standard /// Every hook has a default implementation that performs the standard
/// recursive traversal, so implementors only override the cases they need. /// recursive traversal, so implementors only override the cases they need.
pub trait Visitor: Sized { pub trait Visitor: Sized {
/// Visit a statement node. Defaults to [`walk_stmt`].
fn visit_stmt(&mut self, stmt: &AstNode<Stmt>) -> LoxResult<()> {
walk_stmt(self, stmt)
}
/// Visit an expression node. Defaults to [`walk_expr`]. /// Visit an expression node. Defaults to [`walk_expr`].
fn visit_expr(&mut self, expr: &AstNode<Expr>) -> LoxResult<()> { fn visit_expr(&mut self, expr: &AstNode) -> LoxResult<()> {
walk_expr(self, expr) walk_expr(self, expr)
} }
@@ -62,70 +57,13 @@ pub trait Visitor: Sized {
} }
} }
/// Recurse into the children of `stmt`, calling back into `visitor`.
pub fn walk_stmt<V: Visitor>(visitor: &mut V, stmt: &AstNode<Stmt>) -> LoxResult<()> {
match &stmt.node {
Stmt::Expression { expression, .. } => visitor.visit_expr(expression),
Stmt::Print { expression, .. } => visitor.visit_expr(expression),
Stmt::VarDeclaration { initializer, .. } => {
if let Some(initializer) = initializer {
visitor.visit_expr(initializer)?;
}
Ok(())
}
Stmt::Return { expression, .. } => visitor.visit_expr(expression),
Stmt::Block { statements, .. } => {
for statement in statements.iter() {
visitor.visit_stmt(statement)?;
}
Ok(())
}
Stmt::If {
condition,
then_branch,
elif_branch,
else_branch,
..
} => {
visitor.visit_expr(condition)?;
visitor.visit_stmt(then_branch)?;
for (elif_condition, elif_body) in elif_branch.iter() {
visitor.visit_expr(elif_condition)?;
visitor.visit_stmt(elif_body)?;
}
if let Some(else_body) = else_branch {
visitor.visit_stmt(else_body)?;
}
Ok(())
}
Stmt::While {
condition, body, ..
} => {
visitor.visit_expr(condition)?;
visitor.visit_stmt(body)
}
Stmt::For {
variable,
condition,
increment,
body,
..
} => {
visitor.visit_stmt(variable)?;
visitor.visit_expr(condition)?;
visitor.visit_stmt(increment)?;
visitor.visit_stmt(body)
}
}
}
/// Recurse into the children of `expr`, calling back into `visitor`. /// Recurse into the children of `expr`, calling back into `visitor`.
pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &AstNode<Expr>) -> LoxResult<()> { pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &AstNode) -> LoxResult<()> {
match &expr.node { match &expr.node {
// A function literal carries an entire sub-tree (its body), so it is // A function literal carries an entire sub-tree (its body), so it is
// not a leaf: hand it to the dedicated function hook. // not a leaf: hand it to the dedicated function hook.
Expr::Literal { value } => { Expr::Literal { value } => {
if let BaseValue::Function(function) = value { if let BaseValue::Function(function) = &**value {
visitor.visit_function(function)?; visitor.visit_function(function)?;
} }
Ok(()) Ok(())
@@ -147,6 +85,57 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &AstNode<Expr>) -> LoxResult
} }
Ok(()) Ok(())
} }
Expr::Print { expression, .. } => visitor.visit_expr(expression),
Expr::VarDeclaration { initializer, .. } => {
if let Some(initializer) = initializer {
visitor.visit_expr(initializer)?;
}
Ok(())
}
Expr::Return { expression, .. } => visitor.visit_expr(expression),
Expr::Block { statements, .. } => {
for statement in statements.iter() {
visitor.visit_expr(statement)?;
}
Ok(())
}
Expr::If {
condition,
then_branch,
elif_branches,
else_branch,
..
} => {
visitor.visit_expr(condition)?;
visitor.visit_expr(then_branch)?;
for (elif_condition, elif_body) in elif_branches.iter() {
visitor.visit_expr(elif_condition)?;
visitor.visit_expr(elif_body)?;
}
if let Some(else_body) = else_branch {
visitor.visit_expr(else_body)?;
}
Ok(())
}
Expr::While {
condition, body, ..
} => {
visitor.visit_expr(condition)?;
visitor.visit_expr(body)
}
Expr::For {
variable,
condition,
increment,
body,
..
} => {
visitor.visit_expr(variable)?;
visitor.visit_expr(condition)?;
visitor.visit_expr(increment)?;
visitor.visit_expr(body)
}
} }
} }
@@ -155,7 +144,7 @@ pub fn walk_function<V: Visitor>(visitor: &mut V, function: &LoxFunction) -> Lox
if let Some(guard) = &function.guard { if let Some(guard) = &function.guard {
visitor.visit_expr(guard)?; visitor.visit_expr(guard)?;
} }
visitor.visit_stmt(&function.body) visitor.visit_expr(&function.body)
} }
#[cfg(test)] #[cfg(test)]
@@ -165,7 +154,7 @@ mod tests {
use crate::frontend::lexer::Lexer; use crate::frontend::lexer::Lexer;
use crate::frontend::parser::Parser; use crate::frontend::parser::Parser;
fn parse(src: &str) -> Vec<AstNode<Stmt>> { fn parse(src: &str) -> Vec<AstNode> {
let tokens = Lexer::new(src.to_string(), 0) let tokens = Lexer::new(src.to_string(), 0)
.scans_tokens() .scans_tokens()
.expect("source should lex"); .expect("source should lex");
@@ -173,29 +162,23 @@ mod tests {
} }
/// A visitor that relies entirely on the default traversal and just counts /// A visitor that relies entirely on the default traversal and just counts
/// how many statement and expression nodes it sees. /// how many nodes it sees.
#[derive(Default)] #[derive(Default)]
struct Counter { struct Counter {
stmts: usize, nodes: usize,
exprs: usize,
} }
impl Visitor for Counter { impl Visitor for Counter {
fn visit_stmt(&mut self, stmt: &AstNode<Stmt>) -> LoxResult<()> { fn visit_expr(&mut self, node: &AstNode) -> LoxResult<()> {
self.stmts += 1; self.nodes += 1;
walk_stmt(self, stmt) walk_expr(self, node)
}
fn visit_expr(&mut self, expr: &AstNode<Expr>) -> LoxResult<()> {
self.exprs += 1;
walk_expr(self, expr)
} }
} }
fn count(src: &str) -> Counter { fn count(src: &str) -> Counter {
let mut counter = Counter::default(); let mut counter = Counter::default();
for stmt in parse(src).iter() { for stmt in parse(src).iter() {
counter.visit_stmt(stmt).unwrap(); counter.visit_expr(stmt).unwrap();
} }
counter counter
} }
@@ -204,19 +187,16 @@ mod tests {
fn counts_every_node_via_default_traversal() { fn counts_every_node_via_default_traversal() {
// 1 + 2 * 3 => Binary(+){ Literal, Binary(*){ Literal, Literal } } // 1 + 2 * 3 => Binary(+){ Literal, Binary(*){ Literal, Literal } }
let counter = count("1 + 2 * 3;"); let counter = count("1 + 2 * 3;");
assert_eq!(counter.stmts, 1); assert_eq!(counter.nodes, 5);
assert_eq!(counter.exprs, 5);
} }
#[test] #[test]
fn descends_into_function_bodies() { fn descends_into_function_bodies() {
// The function body must be traversed through `visit_function`, so the // The function body must be traversed through `visit_function`, so the
// `return a;` inside it should contribute to the counts. // `return a;` inside it should contribute to the counts.
let counter = count("f :: fn (a) do return a; end"); let counter = count("f :: fn (a): Number do return a; end;");
// VarDeclaration + Block + Return // VarDeclaration + Function literal + Block + Return + identifier `a`
assert_eq!(counter.stmts, 3); assert_eq!(counter.nodes, 5);
// Function literal + identifier `a`
assert_eq!(counter.exprs, 2);
} }
/// A visitor that aborts as soon as it sees an identifier, used to check /// A visitor that aborts as soon as it sees an identifier, used to check
@@ -224,7 +204,7 @@ mod tests {
struct FailOnIdentifier; struct FailOnIdentifier;
impl Visitor for FailOnIdentifier { impl Visitor for FailOnIdentifier {
fn visit_expr(&mut self, expr: &AstNode<Expr>) -> LoxResult<()> { fn visit_expr(&mut self, expr: &AstNode) -> LoxResult<()> {
if let Expr::Identifier { .. } = &expr.node { if let Expr::Identifier { .. } = &expr.node {
return runtime_error(expr.source_slice.clone(), "found an identifier"); return runtime_error(expr.source_slice.clone(), "found an identifier");
} }
@@ -238,7 +218,7 @@ mod tests {
let mut visitor = FailOnIdentifier; let mut visitor = FailOnIdentifier;
let mut result = Ok(()); let mut result = Ok(());
for stmt in stmts.iter() { for stmt in stmts.iter() {
result = visitor.visit_stmt(stmt); result = visitor.visit_expr(stmt);
if result.is_err() { if result.is_err() {
break; break;
} }
+27 -29
View File
@@ -4,14 +4,14 @@
//! assert on the resulting AST, complementing the per-module unit tests inside //! assert on the resulting AST, complementing the per-module unit tests inside
//! `src/frontend/{lexer,parser}.rs`. //! `src/frontend/{lexer,parser}.rs`.
use rlox::common::ast::{AstNode, Expr, Stmt}; use rlox::common::ast::{AstNode, Expr};
use rlox::common::base_value::{BaseValue, Number}; use rlox::common::base_value::{BaseValue, Number};
use rlox::frontend::lexer::Lexer; use rlox::frontend::lexer::Lexer;
use rlox::frontend::parser::Parser; use rlox::frontend::parser::Parser;
use rlox::frontend::tokens::TokenType; use rlox::frontend::tokens::TokenType;
/// Run the full frontend pipeline on `src`. /// Run the full frontend pipeline on `src`.
fn parse(src: &str) -> Result<Vec<AstNode<Stmt>>, String> { fn parse(src: &str) -> Result<Vec<AstNode>, String> {
let tokens = Lexer::new(src.to_string(), 0) let tokens = Lexer::new(src.to_string(), 0)
.scans_tokens() .scans_tokens()
.map_err(|e| format!("lex error: {e}"))?; .map_err(|e| format!("lex error: {e}"))?;
@@ -21,23 +21,26 @@ fn parse(src: &str) -> Result<Vec<AstNode<Stmt>>, String> {
} }
/// Parse `src`, panicking if the frontend reports any error. /// Parse `src`, panicking if the frontend reports any error.
fn parse_ok(src: &str) -> Vec<AstNode<Stmt>> { fn parse_ok(src: &str) -> Vec<AstNode> {
parse(src).expect("expected source to lex and parse") parse(src).expect("expected source to lex and parse")
} }
/// Parse `src` and return the single statement it should produce. /// Parse `src` and return the single node it should produce.
fn single_stmt(src: &str) -> Stmt { ///
/// Statements and expressions share the unified `Expr`/`AstNode` type, so this
/// simply returns the node of the sole top-level statement.
fn single_stmt(src: &str) -> Expr {
let mut stmts = parse_ok(src); let mut stmts = parse_ok(src);
assert_eq!(stmts.len(), 1, "expected exactly one statement for {src:?}"); assert_eq!(stmts.len(), 1, "expected exactly one statement for {src:?}");
stmts.remove(0).node stmts.remove(0).node
} }
/// Parse `src` and return the expression of its single expression-statement. /// Parse `src` and return its single expression-statement's expression.
///
/// Expression statements aren't wrapped in a dedicated variant anymore; the
/// node itself is the expression.
fn single_expr(src: &str) -> Expr { fn single_expr(src: &str) -> Expr {
match single_stmt(src) { single_stmt(src)
Stmt::Expression { expression, .. } => expression.node,
other => panic!("expected expression statement, got {other:?}"),
}
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -47,7 +50,7 @@ fn single_expr(src: &str) -> Expr {
#[test] #[test]
fn lexes_and_parses_number_literal() { fn lexes_and_parses_number_literal() {
match single_expr("42;") { match single_expr("42;") {
Expr::Literal { value } => assert_eq!(value, BaseValue::Number(Number::I32(42))), Expr::Literal { value } => assert_eq!(*value, BaseValue::Number(Number::I32(42))),
other => panic!("expected literal, got {other:?}"), other => panic!("expected literal, got {other:?}"),
} }
} }
@@ -194,7 +197,7 @@ fn assignment_to_non_identifier_is_an_error() {
#[test] #[test]
fn parses_var_declaration() { fn parses_var_declaration() {
match single_stmt("x := 5;") { match single_stmt("x := 5;") {
Stmt::VarDeclaration { Expr::VarDeclaration {
name, initializer, .. name, initializer, ..
} => { } => {
assert_eq!(name, "x"); assert_eq!(name, "x");
@@ -206,13 +209,13 @@ fn parses_var_declaration() {
#[test] #[test]
fn parses_print_statement() { fn parses_print_statement() {
assert!(matches!(single_stmt("print 1;"), Stmt::Print { .. })); assert!(matches!(single_stmt("print 1;"), Expr::Print { .. }));
} }
#[test] #[test]
fn parses_block_with_multiple_statements() { fn parses_block_with_multiple_statements() {
match single_stmt("do print 1; print 2; end") { match single_stmt("do print 1; print 2; end") {
Stmt::Block { statements, .. } => assert_eq!(statements.len(), 2), Expr::Block { statements, .. } => assert_eq!(statements.len(), 2),
other => panic!("expected block, got {other:?}"), other => panic!("expected block, got {other:?}"),
} }
} }
@@ -220,7 +223,7 @@ fn parses_block_with_multiple_statements() {
#[test] #[test]
fn parses_if_else() { fn parses_if_else() {
match single_stmt("if true then print 1; else print 2;") { match single_stmt("if true then print 1; else print 2;") {
Stmt::If { Expr::If {
else_branch: Some(_), else_branch: Some(_),
.. ..
} => {} } => {}
@@ -232,37 +235,32 @@ fn parses_if_else() {
fn parses_while_loop() { fn parses_while_loop() {
assert!(matches!( assert!(matches!(
single_stmt("while true do print 1; end"), single_stmt("while true do print 1; end"),
Stmt::While { .. } Expr::While { .. }
)); ));
} }
#[test] #[test]
fn parses_for_loop_with_assignment_increment() { fn parses_for_loop_with_assignment_increment() {
match single_stmt("for i := 0; i <= 2; i = i + 1; do print i; end") { match single_stmt("for i := 0; i <= 2; i = i + 1; do print i; end") {
Stmt::For { increment, .. } => match increment.node { Expr::For { increment, .. } => {
// The increment desugars to an expression statement holding an assignment. // The increment is an assignment expression.
Stmt::Expression { expression, .. } => { assert!(matches!(increment.node, Expr::Assign { .. }))
assert!(matches!(expression.node, Expr::Assign { .. })) }
}
other => panic!("expected expression-statement increment, got {other:?}"),
},
other => panic!("expected for loop, got {other:?}"), other => panic!("expected for loop, got {other:?}"),
} }
} }
#[test] #[test]
fn parses_function_declaration() { fn parses_function_declaration() {
match single_stmt("add :: fn (a, b) do return a + b; end") { match single_stmt("add :: fn (a, b): Number do return a + b; end;") {
Stmt::VarDeclaration { Expr::VarDeclaration {
name, initializer, .. name, initializer, ..
} => { } => {
assert_eq!(name, "add"); assert_eq!(name, "add");
match initializer { match initializer {
Some(init) => assert!(matches!( Some(init) => assert!(matches!(
init.node, &init.node,
Expr::Literal { Expr::Literal { value } if matches!(&**value, BaseValue::Function(_))
value: BaseValue::Function(_)
}
)), )),
None => panic!("expected a function initializer"), None => panic!("expected a function initializer"),
} }
+4 -4
View File
@@ -37,13 +37,13 @@ fn resolved_nested_closure_reads_captured_local() {
// `count` is read from inside a nested function, two scopes up; the // `count` is read from inside a nested function, two scopes up; the
// resolver records distance 2 and the interpreter uses `get_at(2, ..)`. // resolver records distance 2 and the interpreter uses `get_at(2, ..)`.
let src = "\ let src = "\
make :: fn () do make :: fn (): Any do
count := 10; count := 10;
get :: fn () do get :: fn (): Number do
return count; return count;
end end;
return get; return get;
end end;
g := make(); g := make();
g(); g();
"; ";