Add parser (and etc.) support for print statements.
This commit is contained in:
@@ -38,33 +38,35 @@ impl FromStr for Program {
|
||||
|
||||
#[test]
|
||||
fn order_of_operations() {
|
||||
let muladd1 = "1 + 2 * 3";
|
||||
let muladd1 = "1 + 2 * 3;";
|
||||
let testfile = 0;
|
||||
assert_eq!(
|
||||
Program::from_str(muladd1).unwrap(),
|
||||
Program {
|
||||
statements: vec![],
|
||||
result: Expression::Primitive(
|
||||
Location::InFile(testfile, 2),
|
||||
"+".to_string(),
|
||||
vec![
|
||||
Expression::Value(Location::InFile(testfile, 0), Value::Number(None, 1)),
|
||||
Expression::Primitive(
|
||||
Location::InFile(testfile, 6),
|
||||
"*".to_string(),
|
||||
vec![
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 4),
|
||||
Value::Number(None, 2),
|
||||
),
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 8),
|
||||
Value::Number(None, 3),
|
||||
),
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
statements: vec![Statement::Expr(
|
||||
Location::InFile(testfile, 0),
|
||||
Expression::Primitive(
|
||||
Location::InFile(testfile, 2),
|
||||
"+".to_string(),
|
||||
vec![
|
||||
Expression::Value(Location::InFile(testfile, 0), Value::Number(None, 1)),
|
||||
Expression::Primitive(
|
||||
Location::InFile(testfile, 6),
|
||||
"*".to_string(),
|
||||
vec![
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 4),
|
||||
Value::Number(None, 2),
|
||||
),
|
||||
Expression::Value(
|
||||
Location::InFile(testfile, 8),
|
||||
Value::Number(None, 3),
|
||||
),
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
),],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ use crate::syntax::token_stream::Location;
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Program {
|
||||
pub statements: Vec<Statement>,
|
||||
pub result: Expression,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Statement {
|
||||
Binding(Location, String, Expression),
|
||||
Print(Location, String),
|
||||
Expr(Location, Expression),
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ extern {
|
||||
"=" => Token::Equals,
|
||||
";" => Token::Semi,
|
||||
|
||||
"print" => Token::Print,
|
||||
|
||||
"+" => Token::Operator('+'),
|
||||
"-" => Token::Operator('-'),
|
||||
"*" => Token::Operator('*'),
|
||||
@@ -24,9 +26,8 @@ extern {
|
||||
}
|
||||
|
||||
pub Program: Program = {
|
||||
<stmts:Statements> <result:Expression> => Program {
|
||||
statements: stmts,
|
||||
result
|
||||
<stmts:Statements> => Program {
|
||||
statements: stmts
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +43,7 @@ Statements: Vec<Statement> = {
|
||||
|
||||
Statement: Statement = {
|
||||
<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),
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ pub enum Token {
|
||||
#[token(";")]
|
||||
Semi,
|
||||
|
||||
#[token("print")]
|
||||
Print,
|
||||
|
||||
#[regex(r"[+\-*/]", |v| v.slice().chars().next())]
|
||||
Operator(char),
|
||||
|
||||
@@ -35,6 +38,7 @@ impl fmt::Display for Token {
|
||||
match self {
|
||||
Token::Equals => write!(f, "'='"),
|
||||
Token::Semi => write!(f, "';'"),
|
||||
Token::Print => write!(f, "'print'"),
|
||||
Token::Operator(c) => write!(f, "'{}'", c),
|
||||
Token::Number((None, v)) => write!(f, "'{}'", v),
|
||||
Token::Number((Some(2), v)) => write!(f, "'0b{:b}'", v),
|
||||
|
||||
Reference in New Issue
Block a user