Skip to content

Commit

Permalink
starting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Dec 6, 2023
1 parent c5e0b32 commit 62bcb45
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub enum Intrinsic {
If(Cmp),
Loop,
Break,
Def,
}

impl Intrinsic {
Expand Down
9 changes: 9 additions & 0 deletions src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ pub fn get_statement<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Statement {
let arg = vec![lhs, rhs];
Statement { comptime, op, arg }
}
(b"def", None) => {
assert_eq!(bytes.next().map(Result::unwrap), Some(b' '));
let ident = get_variable(bytes);
Statement {
comptime,
op: Op::Intrinsic(Intrinsic::Def),
arg: vec![Value::Variable(ident)],
}
}
_ => {
let lhs = Value::Variable(variable);
assert_eq!(
Expand Down
47 changes: 47 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,53 @@ mod tests {
);
}

#[test]
fn fourteen() {
const SOURCE: &str = "def add\n out := in[0] + in[1]\nx := add 1 2\nexit x";

// Parsing
let nodes = test_parsing(
SOURCE,
&[
Statement {
comptime: false,
op: Op::Intrinsic(Intrinsic::Def),
arg: vec![Value::Variable(Variable::new("add"))],
},
Statement {
comptime: false,
op: Op::Intrinsic(Intrinsic::Add),
arg: vec![
Value::Variable(Variable::new("out")),
Value::Variable(Variable {
identifier: Vec::from(b"in"),
index: Some(Box::new(Index::Offset(Offset::Integer(0)))),
}),
Value::Variable(Variable {
identifier: Vec::from(b"in"),
index: Some(Box::new(Index::Offset(Offset::Integer(1)))),
}),
],
},
Statement {
comptime: false,
op: Op::Intrinsic(Intrinsic::Assign),
arg: vec![
Value::Variable(Variable::new("x")),
Value::Variable(Variable::new("add")),
Value::Literal(Literal::Integer(1)),
Value::Literal(Literal::Integer(2)),
],
},
Statement {
comptime: false,
op: Op::Syscall(Syscall::Exit),
arg: vec![Value::Variable(Variable::new("x"))],
},
],
);
}

#[cfg(feature = "false")]
fn thirteen() {
const THIRTEEN: &str = "x := memfd_create\nexit 0";
Expand Down

0 comments on commit 62bcb45

Please sign in to comment.