Add support for syntax blocks.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
x = 1;
|
x = 1;
|
||||||
function add_x(y) x + y
|
function add_x(y) x + y;
|
||||||
a = 3;
|
a = 3;
|
||||||
print x;
|
print x;
|
||||||
result = add_x(a);
|
result = add_x(a);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
x = 1;
|
x = 1;
|
||||||
function add_x(y) x + y
|
function add_x(y) x + y;
|
||||||
a = 3;
|
a = 3;
|
||||||
function add_x_twice(y) add_x(y) + x
|
function add_x_twice(y) add_x(y) + x
|
||||||
print x;
|
print x;
|
||||||
|
|||||||
11
examples/basic/function0003.ngr
Normal file
11
examples/basic/function0003.ngr
Normal 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;
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
x = 4u64;
|
x = 4u64;
|
||||||
function f(y) (x + y)
|
function f(y) (x + y);
|
||||||
print x;
|
print x;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
n = (49u8 + 155u8);
|
n = (49u8 + 155u8);
|
||||||
q = n;
|
q = n;
|
||||||
function u (b) n + b
|
function u (b) n + b;
|
||||||
v = n;
|
v = n;
|
||||||
@@ -95,6 +95,7 @@ impl fmt::Display for Name {
|
|||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Binding(Location, Name, Expression),
|
Binding(Location, Name, Expression),
|
||||||
Print(Location, Name),
|
Print(Location, Name),
|
||||||
|
Expression(Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Statement {
|
impl PartialEq for Statement {
|
||||||
@@ -108,6 +109,10 @@ impl PartialEq for Statement {
|
|||||||
Statement::Print(_, name2) => name1 == name2,
|
Statement::Print(_, name2) => name1 == name2,
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
|
Statement::Expression(e1) => match other {
|
||||||
|
Statement::Expression(e2) => e1 == e2,
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ impl Statement {
|
|||||||
stdout.push_str(&line);
|
stdout.push_str(&line);
|
||||||
Ok(Value::Void)
|
Ok(Value::Void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Statement::Expression(e) => e.eval(stdout, env),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ pub TopLevel: TopLevel = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Function: 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),
|
TopLevel::Function(opt_name, args, exp),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,6 +154,10 @@ Statement: Statement = {
|
|||||||
Location::new(file_idx, ls..le),
|
Location::new(file_idx, ls..le),
|
||||||
Name::new(v, Location::new(file_idx, name_start..name_end)),
|
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
|
// Expressions! Expressions are a little fiddly, because we're going to
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ impl Statement {
|
|||||||
.text("print")
|
.text("print")
|
||||||
.append(allocator.space())
|
.append(allocator.space())
|
||||||
.append(allocator.text(var.to_string())),
|
.append(allocator.text(var.to_string())),
|
||||||
|
Statement::Expression(e) => e.pretty(allocator),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,6 +177,13 @@ impl Statement {
|
|||||||
Statement::Print(loc, var) => {
|
Statement::Print(loc, var) => {
|
||||||
errors.push(Error::UnboundVariable(loc.clone(), var.to_string()))
|
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)
|
(errors, warnings)
|
||||||
|
|||||||
@@ -163,6 +163,10 @@ fn convert_statement(
|
|||||||
|
|
||||||
ir::Expression::Bind(loc, final_name, ty, Box::new(expr))
|
ir::Expression::Bind(loc, final_name, ty, Box::new(expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syntax::Statement::Expression(e) => {
|
||||||
|
convert_expression(e, constraint_db, renames, bindings).0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user