Skip to content

Commit

Permalink
Add parser
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Nov 13, 2024
1 parent 35b4e04 commit 409ebb1
Show file tree
Hide file tree
Showing 4 changed files with 575 additions and 1 deletion.
37 changes: 37 additions & 0 deletions scopegraphs/examples/overload/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[derive(Debug, Clone)]
pub struct Program {
pub functions: Vec<Function>,
pub main: Expr,
}

#[derive(Debug, Clone)]
pub struct Function {
pub name: String,
pub args: Vec<Arg>,
pub return_type: Option<Type>,
pub body: Expr,
}

#[derive(Debug, Clone)]
pub struct Arg {
pub name: String,
pub type_ann: Option<Type>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
IntT,
BoolT,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
IntLit(u64),
BoolLit(bool),
Ident(String),
Plus(Box<Expr>, Box<Expr>),
Lt(Box<Expr>, Box<Expr>),
IfThenElse(Box<Expr>, Box<Expr>, Box<Expr>),
FunCall(String, Vec<Expr>),
Ascribe(Box<Expr>, Type),
}
43 changes: 42 additions & 1 deletion scopegraphs/examples/overload/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
#![allow(unused)]

use crate::ast::*;
use crate::parse::parse;

mod ast;
mod parse;
mod union_find;

pub fn main() {
println!("Hello from overload example!")
let program = "
fun tt() = true;
fun not(b) = if b { false } else { true };
fun and(b1: bool, b2): bool =
if b1 {
if b2 {
true
} else {
false
}
} else {
false
};
$ and(not(false), tt())
";

assert!(parse(program).is_ok())
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PartialType {
// Types are not recursive, so no need to have separate constructors for each variant
Type(Type),
Variable(TypeVar),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TypeVar(usize);

pub struct FunType {
return_type: PartialType,
arg_types: Vec<(String, PartialType)>,
}
Loading

0 comments on commit 409ebb1

Please sign in to comment.