Add support for syntax blocks.

This commit is contained in:
2024-03-06 09:50:42 -08:00
parent 8dab797c90
commit ac564e6e41
11 changed files with 39 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
x = 1;
function add_x(y) x + y
function add_x(y) x + y;
a = 3;
print x;
result = add_x(a);

View File

@@ -1,5 +1,5 @@
x = 1;
function add_x(y) x + y
function add_x(y) x + y;
a = 3;
function add_x_twice(y) add_x(y) + x
print x;

View File

@@ -0,0 +1,11 @@
x = 1u64;
function mean_x(y) {
base = x + y;
result = base / 2;
result;
};
a = 3;
mean_x_and_a = mean_x(a);
mean_x_and_9 = mean_x(9);
print mean_x_and_a;
print mean_x_and_9;

View File

@@ -1,3 +1,3 @@
x = 4u64;
function f(y) (x + y)
function f(y) (x + y);
print x;

View File

@@ -1,4 +1,4 @@
n = (49u8 + 155u8);
q = n;
function u (b) n + b
function u (b) n + b;
v = n;

View File

@@ -95,6 +95,7 @@ impl fmt::Display for Name {
pub enum Statement {
Binding(Location, Name, Expression),
Print(Location, Name),
Expression(Expression),
}
impl PartialEq for Statement {
@@ -108,6 +109,10 @@ impl PartialEq for Statement {
Statement::Print(_, name2) => name1 == name2,
_ => false,
},
Statement::Expression(e1) => match other {
Statement::Expression(e2) => e1 == e2,
_ => false,
},
}
}
}

View File

@@ -69,6 +69,8 @@ impl Statement {
stdout.push_str(&line);
Ok(Value::Void)
}
Statement::Expression(e) => e.eval(stdout, env),
}
}
}

View File

@@ -77,7 +77,7 @@ pub TopLevel: TopLevel = {
}
Function: TopLevel = {
"function" <opt_name:OptionalName> "(" <args:Arguments> OptionalComma ")" <exp:Expression> =>
"function" <opt_name:OptionalName> "(" <args:Arguments> OptionalComma ")" <exp:Expression> ";" =>
TopLevel::Function(opt_name, args, exp),
}
@@ -154,6 +154,10 @@ Statement: Statement = {
Location::new(file_idx, ls..le),
Name::new(v, Location::new(file_idx, name_start..name_end)),
),
// A statement can just be an expression.
<e: Expression> ";" =>
Statement::Expression(e),
}
// Expressions! Expressions are a little fiddly, because we're going to

View File

@@ -56,6 +56,7 @@ impl Statement {
.text("print")
.append(allocator.space())
.append(allocator.text(var.to_string())),
Statement::Expression(e) => e.pretty(allocator),
}
}
}

View File

@@ -177,6 +177,13 @@ impl Statement {
Statement::Print(loc, var) => {
errors.push(Error::UnboundVariable(loc.clone(), var.to_string()))
}
Statement::Expression(e) => {
let (mut exp_errors, mut exp_warnings) = e.validate(bound_variables);
errors.append(&mut exp_errors);
warnings.append(&mut exp_warnings);
}
}
(errors, warnings)

View File

@@ -163,6 +163,10 @@ fn convert_statement(
ir::Expression::Bind(loc, final_name, ty, Box::new(expr))
}
syntax::Statement::Expression(e) => {
convert_expression(e, constraint_db, renames, bindings).0
}
}
}