From 8aa1465c3559d5fa1760571a2071a6c47a42ca72 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Fri, 12 May 2023 17:43:20 -0700 Subject: [PATCH] Prep the way for type casting. --- src/ir/from_syntax.rs | 2 ++ src/syntax/ast.rs | 5 +++++ src/syntax/eval.rs | 2 ++ src/syntax/parser.lalrpop | 8 ++++++++ src/syntax/pretty.rs | 4 ++++ src/syntax/validate.rs | 1 + 6 files changed, 22 insertions(+) diff --git a/src/ir/from_syntax.rs b/src/ir/from_syntax.rs index 46c7c69..98f3ad3 100644 --- a/src/ir/from_syntax.rs +++ b/src/ir/from_syntax.rs @@ -104,6 +104,8 @@ impl syntax::Expression { (vec![], ValueOrRef::Ref(loc, ArcIntern::new(name))) } + syntax::Expression::Cast(_, _, _) => unimplemented!(), + // Primitive expressions are where we do the real work. syntax::Expression::Primitive(loc, prim, mut expressions) => { // generate a fresh new name for the binding site we're going to diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index d71e872..c84e544 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -58,6 +58,7 @@ impl PartialEq for Statement { pub enum Expression { Value(Location, Value), Reference(Location, String), + Cast(Location, String, Box), Primitive(Location, String, Vec), } @@ -72,6 +73,10 @@ impl PartialEq for Expression { Expression::Reference(_, var2) => var1 == var2, _ => false, }, + Expression::Cast(_, t1, e1) => match other { + Expression::Cast(_, t2, e2) => t1 == t2 && e1 == e2, + _ => false, + } Expression::Primitive(_, prim1, args1) => match other { Expression::Primitive(_, prim2, args2) => prim1 == prim2 && args1 == args2, _ => false, diff --git a/src/syntax/eval.rs b/src/syntax/eval.rs index 6504e26..276b242 100644 --- a/src/syntax/eval.rs +++ b/src/syntax/eval.rs @@ -48,6 +48,8 @@ impl Expression { Expression::Reference(_, n) => Ok(env.lookup(ArcIntern::new(n.clone()))?), + Expression::Cast(_, _, _) => unimplemented!(), + Expression::Primitive(_, op, args) => { let mut arg_values = Vec::with_capacity(args.len()); diff --git a/src/syntax/parser.lalrpop b/src/syntax/parser.lalrpop index b6ef647..7694a4e 100644 --- a/src/syntax/parser.lalrpop +++ b/src/syntax/parser.lalrpop @@ -32,6 +32,8 @@ extern { ";" => Token::Semi, "(" => Token::LeftParen, ")" => Token::RightParen, + "<" => Token::LessThan, + ">" => Token::GreaterThan, "print" => Token::Print, @@ -136,6 +138,12 @@ MultiplicativeExpression: Expression = { AtomicExpression, } +UnaryExpression: Expression = { + "-" => Expression::Primitive(Location::new(file_idx, l), "-".to_string(), vec![e]), + "<" "> ">" => Expression::Cast(Location::new(file_idx, l), v.to_string(), Box::new(e)), + AtomicExpression, +} + // finally, we describe our lowest-level expressions as "atomic", because // they cannot be further divided into parts AtomicExpression: Expression = { diff --git a/src/syntax/pretty.rs b/src/syntax/pretty.rs index 46a59fb..4f86613 100644 --- a/src/syntax/pretty.rs +++ b/src/syntax/pretty.rs @@ -50,6 +50,10 @@ where match self { Expression::Value(_, val) => val.pretty(allocator), Expression::Reference(_, var) => allocator.text(var.to_string()), + Expression::Cast(_, t, e) => + allocator.text(t.clone()) + .angles() + .append(e.pretty(allocator)), Expression::Primitive(_, op, exprs) if BINARY_OPERATORS.contains(&op.as_ref()) => { assert_eq!( exprs.len(), diff --git a/src/syntax/validate.rs b/src/syntax/validate.rs index c318e93..feb4ea4 100644 --- a/src/syntax/validate.rs +++ b/src/syntax/validate.rs @@ -127,6 +127,7 @@ impl Expression { vec![Error::UnboundVariable(loc.clone(), var.clone())], vec![], ), + Expression::Cast(_, _, _) => unimplemented!(), Expression::Primitive(_, _, args) => { let mut errors = vec![]; let mut warnings = vec![];