feat: store tokens in ast for basic nodes

master
Araozu 2024-08-13 08:04:01 -05:00
parent d999b8ecfd
commit e52176f90c
9 changed files with 58 additions and 42 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
target target
examples examples
tarpaulin-report.html tarpaulin-report.html
run.sh

View File

@ -74,7 +74,7 @@ impl Typed for Expression<'_> {
}; };
// Only supported unary operator: - & ! // Only supported unary operator: - & !
if *op == "-" { if op.value == "-" {
if !expr_type.is_value("Int") && !expr_type.is_value("Float") { if !expr_type.is_value("Int") && !expr_type.is_value("Float") {
return Err(MistiError::Semantic(SemanticError { return Err(MistiError::Semantic(SemanticError {
error_start: 0, error_start: 0,
@ -87,7 +87,7 @@ impl Typed for Expression<'_> {
} else { } else {
return Ok(Type::Value("Int".into())); return Ok(Type::Value("Int".into()));
} }
} else if *op == "!" { } else if op.value == "!" {
if !expr_type.is_value("Bool") { if !expr_type.is_value("Bool") {
return Err(MistiError::Semantic(SemanticError { return Err(MistiError::Semantic(SemanticError {
error_start: 0, error_start: 0,
@ -99,16 +99,16 @@ impl Typed for Expression<'_> {
} }
} }
unreachable!("Illegal state: Found an unexpected unary operator during semantic analysis: {}", *op); unreachable!("Illegal state: Found an unexpected unary operator during semantic analysis: {}", op.value);
} }
Expression::BinaryOperator(exp1, exp2, operator) => { Expression::BinaryOperator(exp1, exp2, operator) => {
let t1 = exp1.get_type(scope)?; let t1 = exp1.get_type(scope)?;
let t2 = exp2.get_type(scope)?; let t2 = exp2.get_type(scope)?;
// TODO: There's definitely a better way to do this // TODO: There's definitely a better way to do this
if *operator == "+" && t1.is_value("Int") && t2.is_value("Int") { if operator.value == "+" && t1.is_value("Int") && t2.is_value("Int") {
return Ok(Type::Value("Int".into())); return Ok(Type::Value("Int".into()));
} else if *operator == "-" && t1.is_value("Int") && t2.is_value("Int") { } else if operator.value == "-" && t1.is_value("Int") && t2.is_value("Int") {
return Ok(Type::Value("Int".into())); return Ok(Type::Value("Int".into()));
} }

View File

@ -6,6 +6,15 @@ pub struct FunctionCall<'a> {
pub arguments: Box<ArgumentsList<'a>>, pub arguments: Box<ArgumentsList<'a>>,
} }
impl Positionable for FunctionCall<'_> {
fn get_position(&self) -> (usize, usize) {
let (start, _) = self.function.get_position();
let (_, end) = self.arguments.get_position();
(start, end)
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct ArgumentsList<'a> { pub struct ArgumentsList<'a> {
pub arguments: Vec<Expression<'a>>, pub arguments: Vec<Expression<'a>>,

View File

@ -72,8 +72,10 @@ pub enum Expression<'a> {
Boolean(&'a Token), Boolean(&'a Token),
Identifier(&'a Token), Identifier(&'a Token),
FunctionCall(FunctionCall<'a>), FunctionCall(FunctionCall<'a>),
UnaryOperator(&'a String, Box<Expression<'a>>), /// operator, right expression
BinaryOperator(Box<Expression<'a>>, Box<Expression<'a>>, &'a String), UnaryOperator(&'a Token, Box<Expression<'a>>),
/// left expression, right expression, operator
BinaryOperator(Box<Expression<'a>>, Box<Expression<'a>>, &'a Token),
} }
impl Positionable for Expression<'_> { impl Positionable for Expression<'_> {
@ -86,8 +88,12 @@ impl Positionable for Expression<'_> {
Expression::Float(id) => (id.position, id.get_end_position()), Expression::Float(id) => (id.position, id.get_end_position()),
Expression::String(id) => (id.position, id.get_end_position()), Expression::String(id) => (id.position, id.get_end_position()),
Expression::Boolean(id) => (id.position, id.get_end_position()), Expression::Boolean(id) => (id.position, id.get_end_position()),
Expression::FunctionCall(_) => (0, 1), Expression::FunctionCall(f) => f.get_position(),
Expression::UnaryOperator(_, _) => (0, 1), Expression::UnaryOperator(operator, exp) => {
let start = operator.position;
let (_, end) = exp.get_position();
(start, end)
}
Expression::BinaryOperator(_, _, _) => (0, 1), Expression::BinaryOperator(_, _, _) => (0, 1),
} }
} }

View File

@ -37,7 +37,7 @@ fn parse_many<'a>(
) { ) {
Ok((expr, next_pos)) => { Ok((expr, next_pos)) => {
let expr = let expr =
Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token.value); Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token);
parse_many(tokens, next_pos, expr, indentation_level + indent_count) parse_many(tokens, next_pos, expr, indentation_level + indent_count)
} }
@ -67,7 +67,7 @@ mod tests {
} }
_ => panic!("Expected 2 identifiers"), _ => panic!("Expected 2 identifiers"),
} }
assert_eq!(">=", op) assert_eq!(">=", op.value)
} }
_ => panic!("Expected a binary expression with 2 identifiers"), _ => panic!("Expected a binary expression with 2 identifiers"),
}, },
@ -98,7 +98,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, ">=") assert_eq!(op.value, ">=")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -112,7 +112,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "<=") assert_eq!(op.value, "<=")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -128,7 +128,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "<=") assert_eq!(op.value, "<=")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -143,7 +143,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "<=") assert_eq!(op.value, "<=")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -158,7 +158,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, ">=") assert_eq!(op.value, ">=")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }

View File

@ -37,7 +37,7 @@ fn parse_many<'a>(
) { ) {
Ok((expr, next_pos)) => { Ok((expr, next_pos)) => {
let expr = let expr =
Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token.value); Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token);
parse_many(tokens, next_pos, expr, indentation_level + indent_count) parse_many(tokens, next_pos, expr, indentation_level + indent_count)
} }
@ -66,7 +66,7 @@ mod tests {
} }
_ => panic!("Expected 2 identifiers"), _ => panic!("Expected 2 identifiers"),
} }
assert_eq!("==", op) assert_eq!("==", op.value)
} }
_ => panic!("Expected a binary expression with 2 identifiers"), _ => panic!("Expected a binary expression with 2 identifiers"),
}, },
@ -97,7 +97,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "==") assert_eq!(op.value, "==")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -114,7 +114,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "==") assert_eq!(op.value, "==")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -130,7 +130,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "==") assert_eq!(op.value, "==")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -145,7 +145,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "==") assert_eq!(op.value, "==")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -160,7 +160,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "==") assert_eq!(op.value, "==")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }

View File

@ -39,7 +39,7 @@ fn parse_many<'a>(
let expr = Expression::BinaryOperator( let expr = Expression::BinaryOperator(
Box::new(prev_expr), Box::new(prev_expr),
Box::new(expr), Box::new(expr),
&token.value, &token,
); );
parse_many(tokens, next_pos, expr, indentation_level + indent_count) parse_many(tokens, next_pos, expr, indentation_level + indent_count)
@ -70,7 +70,7 @@ mod tests {
} }
_ => panic!("Expected 2 identifiers"), _ => panic!("Expected 2 identifiers"),
} }
assert_eq!("*", op) assert_eq!("*", op.value)
} }
_ => panic!("Expected a binary expression with 2 identifiers"), _ => panic!("Expected a binary expression with 2 identifiers"),
}, },
@ -101,7 +101,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "*") assert_eq!(op.value, "*")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -118,7 +118,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "*") assert_eq!(op.value, "*")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -134,7 +134,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "*") assert_eq!(op.value, "*")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -149,7 +149,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "*") assert_eq!(op.value, "*")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -164,7 +164,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "/") assert_eq!(op.value, "/")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -179,7 +179,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "/") assert_eq!(op.value, "/")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }

View File

@ -39,7 +39,7 @@ fn parse_many<'a>(
let expr = Expression::BinaryOperator( let expr = Expression::BinaryOperator(
Box::new(prev_expr), Box::new(prev_expr),
Box::new(expr), Box::new(expr),
&token.value, &token,
); );
parse_many(tokens, next_pos, expr, indentation_level + indent_count) parse_many(tokens, next_pos, expr, indentation_level + indent_count)
@ -71,7 +71,7 @@ mod tests {
} }
_ => panic!("Expected 2 identifiers"), _ => panic!("Expected 2 identifiers"),
} }
assert_eq!("+", op) assert_eq!("+", op.value)
} }
_ => panic!("Expected a binary expression with 2 identifiers"), _ => panic!("Expected a binary expression with 2 identifiers"),
}, },
@ -102,7 +102,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -116,7 +116,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -132,7 +132,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -147,7 +147,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -162,7 +162,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }
@ -177,7 +177,7 @@ mod tests {
match result { match result {
Expression::BinaryOperator(_, _, op) => { Expression::BinaryOperator(_, _, op) => {
assert_eq!(op, "+") assert_eq!(op.value, "+")
} }
_ => panic!("Expected a binary operator"), _ => panic!("Expected a binary operator"),
} }

View File

@ -16,7 +16,7 @@ pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
Some(token) if token.value == "!" || token.value == "-" => { Some(token) if token.value == "!" || token.value == "-" => {
match Expression::try_parse(tokens, pos + 1) { match Expression::try_parse(tokens, pos + 1) {
Ok((expression, next_pos)) => Ok(( Ok((expression, next_pos)) => Ok((
Expression::UnaryOperator(&token.value, Box::new(expression)), Expression::UnaryOperator(&token, Box::new(expression)),
next_pos, next_pos,
)), )),
_ => Err(ParsingError::Unmatched), _ => Err(ParsingError::Unmatched),
@ -53,7 +53,7 @@ mod tests {
Ok((Expression::UnaryOperator(operator, expression), _)) => { Ok((Expression::UnaryOperator(operator, expression), _)) => {
match (operator, *expression) { match (operator, *expression) {
(op, Expression::Int(value)) => { (op, Expression::Int(value)) => {
assert_eq!(*op, "-"); assert_eq!(op.value, "-");
assert_eq!(value.value, "10"); assert_eq!(value.value, "10");
} }
_ => panic!("unexpected values"), _ => panic!("unexpected values"),
@ -70,7 +70,7 @@ mod tests {
match expression { match expression {
Ok((Expression::UnaryOperator(operator, expression), _)) => { Ok((Expression::UnaryOperator(operator, expression), _)) => {
assert_eq!(*operator, "-"); assert_eq!(operator.value, "-");
match *expression { match *expression {
Expression::BinaryOperator(_, _, _) => { Expression::BinaryOperator(_, _, _) => {
// :D // :D