Add expressions

This commit is contained in:
bad 2022-04-23 19:43:42 +02:00
parent 3a5d81c8fb
commit 9cda9643a9
10 changed files with 57 additions and 9 deletions

View file

@ -120,6 +120,7 @@ pub enum Literal {
Int(i32),
Float(f32),
Bool(bool),
Nil
}
pub struct BinaryExpr {

View file

@ -102,6 +102,7 @@ impl<'a, T: Iterator<Item = token::Token<'a>>> Parser<'a, T> {
TokenType::Int(i) => ExpressionNode::Literal(Literal::Int(i)),
TokenType::String(i) => ExpressionNode::Literal(Literal::String(i)),
TokenType::Float(f) => ExpressionNode::Literal(Literal::Float(f)),
TokenType::Nil => ExpressionNode::Literal(Literal::Nil),
TokenType::LeftParen => {
let expr = self.expression()?;
let group = GroupingExpr::new(Box::new(expr));

View file

@ -7,8 +7,19 @@ pub enum Statement {
Print(PrintStatement),
}
macro_rules! all_variants {
($expr:expr, $val_name:ident => $expr_arm:expr) => {
{
use match_any::match_any;
use $crate::ast::statement::statement_node::*;
match_any!($expr, Statement::Expression($val_name) | Statement::Print($val_name) => $expr_arm)
}
};
}
pub(crate) use all_variants;
#[derive(Debug)]
pub struct ExpressionStatement(ExpressionNode);
pub struct ExpressionStatement(pub ExpressionNode);
impl ExpressionStatement {
pub fn new(expr: ExpressionNode) -> Self {
Self(expr)
@ -16,7 +27,7 @@ impl ExpressionStatement {
}
#[derive(Debug)]
pub struct PrintStatement(ExpressionNode);
pub struct PrintStatement(pub ExpressionNode);
impl PrintStatement {
pub fn new(expr: ExpressionNode) -> Self {
Self(expr)

View file

@ -1,4 +1,5 @@
mod expression_interpreter;
mod statement_interpreter;
pub use super::{error::RuntimeError, types};
use super::types::Value;

View file

@ -0,0 +1,26 @@
use super::{RuntimeError, types::Value};
use super::Interpret;
use crate::ast::statement::statement_node;
impl Interpret for statement_node::Statement {
fn interpret(&self) -> Result<Value, RuntimeError> {
statement_node::all_variants!(self, n => n.interpret())
}
}
impl Interpret for statement_node::PrintStatement {
fn interpret(&self) -> Result<Value, RuntimeError> {
let res = self.0.interpret()?;
println!("{:?}", res);
Ok(res)
}
}
impl Interpret for statement_node::ExpressionStatement {
fn interpret(&self) -> Result<Value, RuntimeError> {
self.0.interpret()
}
}

View file

@ -0,0 +1,3 @@
struct Environment {
}

View file

@ -1,3 +1,4 @@
pub mod ast_walker;
pub mod types;
pub mod error;
mod environment;

View file

@ -6,7 +6,7 @@ pub enum Value {
Int(i32),
Float(f32),
Bool(bool),
Null,
Nil,
// TODO: create a cow(not rust cow) string instead of cloning a normal string
String(String),
}
@ -14,14 +14,15 @@ pub enum Value {
impl From<expression_node::Literal> for Value {
fn from(l: expression_node::Literal) -> Self {
match_any::match_any!(l,
expression_node::Literal::Int(v) | expression_node::Literal::Bool(v) | expression_node::Literal::Float(v) | expression_node::Literal::String(v) => v.into()
expression_node::Literal::Int(v) | expression_node::Literal::Bool(v) | expression_node::Literal::Float(v) | expression_node::Literal::String(v) => v.into(),
expression_node::Literal::Nil => Self::Nil
)
}
}
impl Default for Value {
fn default() -> Self {
Self::Null
Self::Nil
}
}

View file

@ -23,12 +23,11 @@ pub fn parse(tokens: Vec<Token>) -> ParseAllResult {
}
pub fn exec(nodes: Vec<Statement>) -> Result<Value, RuntimeError> {
todo!();
/*
let mut last_res = Value::Nil;
for statement in nodes {
//statement.interpret()?;
last_res = statement.interpret()?;
}
*/
Ok(last_res)
}
pub fn run(code: &str) -> Result<Value, run::Error> {

View file

@ -1 +1,5 @@
1 == 1;
1 == 2;
print 1;
print 2;
nil;