Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload example #64

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, Hash)]
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),
}
48 changes: 47 additions & 1 deletion scopegraphs/examples/overload/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
#![allow(unused)]

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

mod ast;
mod parse;
mod type_check;
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())
";

let result = parse(program);

println!("{:?}", result);

assert!(result.is_ok())
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
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, Hash)]
pub struct TypeVar(usize);

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