Skip to content

Commit 409ebb1

Browse files
committed
Add parser
1 parent 35b4e04 commit 409ebb1

File tree

4 files changed

+575
-1
lines changed

4 files changed

+575
-1
lines changed

scopegraphs/examples/overload/ast.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[derive(Debug, Clone)]
2+
pub struct Program {
3+
pub functions: Vec<Function>,
4+
pub main: Expr,
5+
}
6+
7+
#[derive(Debug, Clone)]
8+
pub struct Function {
9+
pub name: String,
10+
pub args: Vec<Arg>,
11+
pub return_type: Option<Type>,
12+
pub body: Expr,
13+
}
14+
15+
#[derive(Debug, Clone)]
16+
pub struct Arg {
17+
pub name: String,
18+
pub type_ann: Option<Type>,
19+
}
20+
21+
#[derive(Debug, Clone, PartialEq, Eq)]
22+
pub enum Type {
23+
IntT,
24+
BoolT,
25+
}
26+
27+
#[derive(Debug, Clone, PartialEq, Eq)]
28+
pub enum Expr {
29+
IntLit(u64),
30+
BoolLit(bool),
31+
Ident(String),
32+
Plus(Box<Expr>, Box<Expr>),
33+
Lt(Box<Expr>, Box<Expr>),
34+
IfThenElse(Box<Expr>, Box<Expr>, Box<Expr>),
35+
FunCall(String, Vec<Expr>),
36+
Ascribe(Box<Expr>, Type),
37+
}

scopegraphs/examples/overload/main.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
#![allow(unused)]
2+
3+
use crate::ast::*;
4+
use crate::parse::parse;
5+
6+
mod ast;
7+
mod parse;
8+
mod union_find;
9+
110
pub fn main() {
2-
println!("Hello from overload example!")
11+
let program = "
12+
fun tt() = true;
13+
fun not(b) = if b { false } else { true };
14+
fun and(b1: bool, b2): bool =
15+
if b1 {
16+
if b2 {
17+
true
18+
} else {
19+
false
20+
}
21+
} else {
22+
false
23+
};
24+
25+
$ and(not(false), tt())
26+
";
27+
28+
assert!(parse(program).is_ok())
29+
}
30+
31+
#[derive(Debug, Clone, PartialEq, Eq)]
32+
pub enum PartialType {
33+
// Types are not recursive, so no need to have separate constructors for each variant
34+
Type(Type),
35+
Variable(TypeVar),
36+
}
37+
38+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39+
pub struct TypeVar(usize);
40+
41+
pub struct FunType {
42+
return_type: PartialType,
43+
arg_types: Vec<(String, PartialType)>,
344
}

0 commit comments

Comments
 (0)