better ast
This commit is contained in:
parent
95fedac802
commit
55605d2cd1
4 changed files with 292 additions and 174 deletions
|
|
@ -1,95 +1,134 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
|
||||
|
||||
/// Abstract Syntax Tree
|
||||
pub type BlockStatement = Vec<Statement>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Expr {
|
||||
Ident(Ident),
|
||||
Literal(Literal),
|
||||
Call(FunctionCall),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FunctionCall {
|
||||
pub parameters: Vec<Expr>,
|
||||
pub name: Rc<str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expression<'src> {
|
||||
// Identifier
|
||||
Ident(&'src str),
|
||||
|
||||
// Types
|
||||
Integer(i64),
|
||||
Float(f64),
|
||||
String(String),
|
||||
pub enum ExprResult {
|
||||
Bool(bool),
|
||||
|
||||
// Operations
|
||||
Negatation(Box<Expression<'src>>),
|
||||
Add(Box<Expression<'src>>, Box<Expression<'src>>),
|
||||
Substract(Box<Expression<'src>>, Box<Expression<'src>>),
|
||||
Multiply(Box<Expression<'src>>, Box<Expression<'src>>),
|
||||
Divide(Box<Expression<'src>>, Box<Expression<'src>>),
|
||||
|
||||
// Keywords
|
||||
Var {
|
||||
name: &'src str,
|
||||
rhs: Box<Expression<'src>>,
|
||||
then: Box<Expression<'src>>,
|
||||
},
|
||||
|
||||
Function {
|
||||
name: &'src str,
|
||||
args: Vec<&'src str>,
|
||||
body: Box<Expression<'src>>,
|
||||
then: Box<Expression<'src>>,
|
||||
},
|
||||
|
||||
Unit,
|
||||
UnsignedInteger(usize),
|
||||
SignedInteger(isize),
|
||||
Char(char),
|
||||
Return(Box<ExprResult>),
|
||||
Void,
|
||||
}
|
||||
|
||||
pub fn eval<'src>(
|
||||
expr: &'src Expression<'src>,
|
||||
vars: &mut Vec<(&'src str, f64)>,
|
||||
funcs: &mut Vec<(&'src str, &'src [&'src str], &'src Expression<'src>)>,
|
||||
) -> Result<f64, String> {
|
||||
match expr {
|
||||
Expression::Ident(name) => {
|
||||
if let Some((_, val)) = vars.iter().rev().find(|(var, _)| var == name) {
|
||||
Ok(*val)
|
||||
} else {
|
||||
Err(format!("Cannot find variable `{name}` in scope"))
|
||||
}
|
||||
},
|
||||
|
||||
// Types
|
||||
Expression::Integer(x) => Ok((*x) as f64), // todo
|
||||
|
||||
Expression::Float(x) => Ok(*x),
|
||||
|
||||
Expression::String(_) => todo!(),
|
||||
|
||||
Expression::Bool(_) => todo!(),
|
||||
|
||||
// Operations
|
||||
Expression::Negatation(lhs) => todo!(),
|
||||
|
||||
Expression::Add(lhs, rhs) => Ok(eval(lhs, vars, funcs)? + eval(rhs, vars, funcs)?),
|
||||
|
||||
Expression::Substract(lhs, rhs) => Ok(eval(lhs, vars, funcs)? - eval(rhs, vars, funcs)?),
|
||||
|
||||
Expression::Multiply(lhs, rhs) => Ok(eval(lhs, vars, funcs)? * eval(rhs, vars, funcs)?),
|
||||
|
||||
Expression::Divide(lhs, rhs) => Ok(eval(lhs, vars, funcs)? / eval(rhs, vars, funcs)?),
|
||||
|
||||
// Keywords
|
||||
Expression::Var { name, rhs, then } => {
|
||||
let rhs = eval(rhs, vars, funcs)?;
|
||||
vars.push((*name, rhs));
|
||||
let output = eval(then, vars, funcs);
|
||||
vars.pop();
|
||||
output
|
||||
},
|
||||
|
||||
Expression::Function {
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
then,
|
||||
} => {
|
||||
funcs.push((name, args, body));
|
||||
let output = eval(then, vars, funcs);
|
||||
funcs.pop();
|
||||
output
|
||||
},
|
||||
|
||||
Expression::Unit => todo!(),
|
||||
impl std::fmt::Display for ExprResult {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ExprResult::Bool(b) => write!(f, "{b}"),
|
||||
ExprResult::UnsignedInteger(i) => write!(f, "{i}"),
|
||||
ExprResult::SignedInteger(i) => write!(f, "{i}"),
|
||||
ExprResult::Char(c) => write!(f, "{c}"),
|
||||
ExprResult::Return(v) => write!(f, "{}", *v),
|
||||
ExprResult::Void => write!(f, ""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum BinaryOp {
|
||||
Multiply,
|
||||
Divide,
|
||||
Add,
|
||||
Substract,
|
||||
|
||||
Equals,
|
||||
NotEquals,
|
||||
Less,
|
||||
LessEquals,
|
||||
Greater,
|
||||
GreaterEquals,
|
||||
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum UnaryOp {
|
||||
Not,
|
||||
Minus,
|
||||
Plus,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Literal {
|
||||
UnsignedInteger(usize),
|
||||
Bool(bool),
|
||||
Char(char),
|
||||
String(Rc<str>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Ident(pub Rc<str>);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Statement {
|
||||
Var(Ident, Option<Type>)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct While {
|
||||
pub condition: Expr,
|
||||
pub body: BlockStatement,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Condition {
|
||||
pub condition: Expr,
|
||||
pub if_body: BlockStatement,
|
||||
pub else_body: Option<BlockStatement>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Type {
|
||||
UnsignedInteger,
|
||||
SignedInteger,
|
||||
Bool,
|
||||
Char,
|
||||
String,
|
||||
}
|
||||
|
||||
pub enum Value {
|
||||
UnsignedInteger(u32),
|
||||
SignedInteger(i32),
|
||||
Bool(bool),
|
||||
Char(char),
|
||||
String(String),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn is_type(&self, ty: &Type) -> bool {
|
||||
match (ty, self) {
|
||||
(Type::Bool, Value::Bool(_)) => true,
|
||||
(Type::Char, Value::Char(_)) => true,
|
||||
(Type::SignedInteger, Value::SignedInteger(_)) => true,
|
||||
(Type::UnsignedInteger, Value::UnsignedInteger(_)) => true,
|
||||
(Type::String, Value::String(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Function {
|
||||
pub name: Rc<str>,
|
||||
pub params: Vec<(Ident, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
pub body: Vec<Statement>,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue