Add parser (and etc.) support for print statements.

This commit is contained in:
2022-02-16 20:52:23 -08:00
parent 0293eee2d0
commit 6c5ca6b782
4 changed files with 35 additions and 27 deletions

View File

@@ -38,13 +38,14 @@ impl FromStr for Program {
#[test] #[test]
fn order_of_operations() { fn order_of_operations() {
let muladd1 = "1 + 2 * 3"; let muladd1 = "1 + 2 * 3;";
let testfile = 0; let testfile = 0;
assert_eq!( assert_eq!(
Program::from_str(muladd1).unwrap(), Program::from_str(muladd1).unwrap(),
Program { Program {
statements: vec![], statements: vec![Statement::Expr(
result: Expression::Primitive( Location::InFile(testfile, 0),
Expression::Primitive(
Location::InFile(testfile, 2), Location::InFile(testfile, 2),
"+".to_string(), "+".to_string(),
vec![ vec![
@@ -65,6 +66,7 @@ fn order_of_operations() {
) )
] ]
) )
),],
} }
); );
} }

View File

@@ -3,12 +3,12 @@ use crate::syntax::token_stream::Location;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Program { pub struct Program {
pub statements: Vec<Statement>, pub statements: Vec<Statement>,
pub result: Expression,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Statement { pub enum Statement {
Binding(Location, String, Expression), Binding(Location, String, Expression),
Print(Location, String),
Expr(Location, Expression), Expr(Location, Expression),
} }

View File

@@ -13,6 +13,8 @@ extern {
"=" => Token::Equals, "=" => Token::Equals,
";" => Token::Semi, ";" => Token::Semi,
"print" => Token::Print,
"+" => Token::Operator('+'), "+" => Token::Operator('+'),
"-" => Token::Operator('-'), "-" => Token::Operator('-'),
"*" => Token::Operator('*'), "*" => Token::Operator('*'),
@@ -24,9 +26,8 @@ extern {
} }
pub Program: Program = { pub Program: Program = {
<stmts:Statements> <result:Expression> => Program { <stmts:Statements> => Program {
statements: stmts, statements: stmts
result
} }
} }
@@ -42,6 +43,7 @@ Statements: Vec<Statement> = {
Statement: Statement = { Statement: Statement = {
<l:@L> <v:"<var>"> "=" <e:Expression> ";" => Statement::Binding(l, v.to_string(), e), <l:@L> <v:"<var>"> "=" <e:Expression> ";" => Statement::Binding(l, v.to_string(), e),
<l:@L> "print" <v:"<var>"> ";" => Statement::Print(l, v.to_string()),
<l:@L> <e:Expression> ";" => Statement::Expr(l, e), <l:@L> <e:Expression> ";" => Statement::Expr(l, e),
} }

View File

@@ -11,6 +11,9 @@ pub enum Token {
#[token(";")] #[token(";")]
Semi, Semi,
#[token("print")]
Print,
#[regex(r"[+\-*/]", |v| v.slice().chars().next())] #[regex(r"[+\-*/]", |v| v.slice().chars().next())]
Operator(char), Operator(char),
@@ -35,6 +38,7 @@ impl fmt::Display for Token {
match self { match self {
Token::Equals => write!(f, "'='"), Token::Equals => write!(f, "'='"),
Token::Semi => write!(f, "';'"), Token::Semi => write!(f, "';'"),
Token::Print => write!(f, "'print'"),
Token::Operator(c) => write!(f, "'{}'", c), Token::Operator(c) => write!(f, "'{}'", c),
Token::Number((None, v)) => write!(f, "'{}'", v), Token::Number((None, v)) => write!(f, "'{}'", v),
Token::Number((Some(2), v)) => write!(f, "'0b{:b}'", v), Token::Number((Some(2), v)) => write!(f, "'0b{:b}'", v),