wip state

This commit is contained in:
Michael Zhang 2022-04-13 02:09:56 -05:00
parent da39c73036
commit 1e954399c6
Signed by untrusted user who does not match committer: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 60 additions and 0 deletions

7
examples/functions.e0 Normal file
View file

@ -0,0 +1,7 @@
fn compute() -> int {
return 42;
}
fn main() -> int {
return compute();
}

View file

@ -72,6 +72,13 @@ impl Expr<Type> {
}
}
}
ExprKind::Call(func, args) => {
let func_llvm = func.into_llvm();
builder.build_call(func_llvm);
todo!()
}
}
}
}

View file

@ -44,6 +44,7 @@ pub enum ExprKind<T> {
Int(i64),
Var(String),
BinOp(Box<Expr<T>>, Op, Box<Expr<T>>),
Call(Box<Expr<T>>, Vec<Expr<T>>),
}
#[derive(Copy, Clone, Debug)]

View file

@ -210,6 +210,25 @@ fn annotate_expr(ctx: &mut AnnotationContext, expr: &Expr<()>) -> Expr<Type_> {
ty: output,
}
}
ExprKind::Call(func, args) => {
let func = annotate_expr(ctx, func);
let mut args_annot = Vec::new();
let ret_ty = ctx.type_var();
for arg in args {
let arg_annot = annotate_expr(ctx, arg);
args_annot.push(arg_annot);
}
// ctx.constrain();
// TODO:
Expr {
kind: ExprKind::Call(Box::new(func), args_annot),
ty: ret_ty,
}
}
}
}
@ -328,6 +347,14 @@ fn substitute_in_expr_kind(
let right = substitute_in_expr(assignments, *right)?;
ExprKind::BinOp(Box::new(left), op, Box::new(right))
}
ExprKind::Call(func, args) => {
let func = substitute_in_expr(assignments, *func)?;
let args = args
.into_iter()
.map(|arg| substitute_in_expr(assignments, arg))
.collect::<Result<_>>()?;
ExprKind::Call(Box::new(func), args)
}
})
}

View file

@ -42,6 +42,10 @@ Expr: Expr<()> = {
#[precedence(level = "0")]
Ident => Expr { kind: ExprKind::Var(<>), ty: () },
#[precedence(level = "2")]
<expr:Expr> "(" <args:Punct<",", Expr>> ")" =>
Expr { kind: ExprKind::Call(Box::new(expr), args), ty: () },
#[precedence(level = "13")]
#[assoc(side = "none")]
<left:Expr> <op:CompareOp> <right:Expr> => Expr {
@ -60,3 +64,17 @@ Type: Type = {
};
Ident: String = r"([A-Za-z][A-Za-z0-9_]*)|(_[A-Za-z0-9_]+)" => <>.to_string();
Punct<P, T>: Vec<T> = {
P => vec![],
<first:T> <rest:(P Punct<P, T>)?> => {
match rest {
Some(rest) => {
let (_, mut vec) = rest;
vec.insert(0, first);
vec
}
None => vec![first],
}
},
};