Autoformat
This commit is contained in:
parent
f6152670aa
commit
588b3b5b2c
@ -1,6 +1,10 @@
|
||||
use std::{collections::HashMap, fmt::Display, rc::Rc};
|
||||
|
||||
use crate::{ast::{Expression, BinOpType, UnOpType, Ast, Statement, If}, parser::parse, lexer::lex};
|
||||
use crate::{
|
||||
ast::{Ast, BinOpType, Expression, If, Statement, UnOpType},
|
||||
lexer::lex,
|
||||
parser::parse,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Value {
|
||||
@ -61,7 +65,11 @@ impl Interpreter {
|
||||
print!("{}", result);
|
||||
}
|
||||
|
||||
Statement::If(If {condition, body_true, body_false}) => {
|
||||
Statement::If(If {
|
||||
condition,
|
||||
body_true,
|
||||
body_false,
|
||||
}) => {
|
||||
if matches!(self.resolve_expr(condition), Value::I64(0)) {
|
||||
self.run(body_false);
|
||||
} else {
|
||||
@ -69,7 +77,6 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,7 +123,7 @@ impl Interpreter {
|
||||
}
|
||||
return rhs;
|
||||
}
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
|
||||
let lhs = self.resolve_expr(lhs);
|
||||
@ -158,11 +165,10 @@ impl Display for Value {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{Interpreter, Value};
|
||||
use crate::ast::{Expression, BinOpType};
|
||||
use crate::ast::{BinOpType, Expression};
|
||||
|
||||
#[test]
|
||||
fn test_interpreter_expr() {
|
||||
@ -173,7 +179,12 @@ mod test {
|
||||
Expression::BinOp(
|
||||
BinOpType::Add,
|
||||
Expression::I64(1).into(),
|
||||
Expression::BinOp(BinOpType::Mul, Expression::I64(2).into(), Expression::I64(3).into()).into(),
|
||||
Expression::BinOp(
|
||||
BinOpType::Mul,
|
||||
Expression::I64(2).into(),
|
||||
Expression::I64(3).into(),
|
||||
)
|
||||
.into(),
|
||||
)
|
||||
.into(),
|
||||
Expression::I64(4).into(),
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@ -1,8 +1,11 @@
|
||||
use std::{env::args, fs, io::{stdout, Write, stdin}};
|
||||
use std::{
|
||||
env::args,
|
||||
fs,
|
||||
io::{stdin, stdout, Write},
|
||||
};
|
||||
|
||||
use nek_lang::interpreter::Interpreter;
|
||||
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct CliConfig {
|
||||
print_tokens: bool,
|
||||
@ -12,7 +15,6 @@ struct CliConfig {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut conf = CliConfig::default();
|
||||
|
||||
// Go through all commandline arguments except the first (filename)
|
||||
@ -49,7 +51,5 @@ fn main() {
|
||||
|
||||
interpreter.run_str(&code, conf.print_tokens, conf.print_ast);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use std::iter::Peekable;
|
||||
|
||||
use crate::token::Token;
|
||||
use crate::ast::*;
|
||||
use crate::token::Token;
|
||||
|
||||
/// Parse the given tokens into an abstract syntax tree
|
||||
pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A) -> Ast {
|
||||
@ -30,20 +30,14 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
Token::Semicolon => {
|
||||
self.next();
|
||||
}
|
||||
Token::EoF => break,
|
||||
Token::RBraces => {
|
||||
break;
|
||||
}
|
||||
Token::EoF | Token::RBraces => break,
|
||||
|
||||
// By default try to lex a statement
|
||||
_ => {
|
||||
prog.push(self.parse_stmt())
|
||||
}
|
||||
_ => prog.push(self.parse_stmt()),
|
||||
}
|
||||
}
|
||||
|
||||
Ast { prog }
|
||||
|
||||
}
|
||||
|
||||
/// Parse a single statement from the tokens.
|
||||
@ -114,7 +108,11 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
}
|
||||
}
|
||||
|
||||
If { condition, body_true, body_false }
|
||||
If {
|
||||
condition,
|
||||
body_true,
|
||||
body_false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a loop statement from the tokens
|
||||
@ -141,17 +139,20 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
}
|
||||
|
||||
body = self.parse();
|
||||
},
|
||||
}
|
||||
|
||||
_ => panic!("Error lexing loop: Expected ';' or '{{'")
|
||||
_ => panic!("Error lexing loop: Expected ';' or '{{'"),
|
||||
}
|
||||
|
||||
if !matches!(self.next(), Token::RBraces) {
|
||||
panic!("Error lexing loop: Expected '}}'")
|
||||
}
|
||||
|
||||
Loop { condition, advancement, body }
|
||||
|
||||
Loop {
|
||||
condition,
|
||||
advancement,
|
||||
body,
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a single expression from the tokens
|
||||
@ -246,8 +247,11 @@ impl<T: Iterator<Item = Token>> Parser<T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{parse, Expression, BinOpType};
|
||||
use crate::{token::Token, parser::{Statement, Ast}};
|
||||
use super::{parse, BinOpType, Expression};
|
||||
use crate::{
|
||||
parser::{Ast, Statement},
|
||||
token::Token,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_parser() {
|
||||
@ -269,13 +273,20 @@ mod tests {
|
||||
Expression::BinOp(
|
||||
BinOpType::Add,
|
||||
Expression::I64(1).into(),
|
||||
Expression::BinOp(BinOpType::Mul, Expression::I64(2).into(), Expression::I64(3).into()).into(),
|
||||
Expression::BinOp(
|
||||
BinOpType::Mul,
|
||||
Expression::I64(2).into(),
|
||||
Expression::I64(3).into(),
|
||||
)
|
||||
.into(),
|
||||
)
|
||||
.into(),
|
||||
Expression::I64(4).into(),
|
||||
));
|
||||
|
||||
let expected = Ast { prog: vec![expected] };
|
||||
let expected = Ast {
|
||||
prog: vec![expected],
|
||||
};
|
||||
|
||||
let actual = parse(tokens);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user