Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from amethyst-lang/rewrite
Browse files Browse the repository at this point in the history
Rewrite
  • Loading branch information
melody-notpond authored Mar 30, 2024
2 parents 3852460 + b3f8d27 commit e61236e
Show file tree
Hide file tree
Showing 28 changed files with 1,665 additions and 2,918 deletions.
44 changes: 0 additions & 44 deletions example.amy

This file was deleted.

143 changes: 143 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use std::fmt::Display;

#[derive(Debug, Clone, PartialEq)]
pub enum Type {
Typevar(usize),
Name(String),
Generic(String),
Func(Vec<Type>, Box<Type>),
App(Box<Type>, Vec<Type>),
}

impl Type {
pub fn func_of_app(&self) -> &Type {
match self {
Type::App(t, _) => t.func_of_app(),
_ => self
}
}
}

impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Typevar(i) => write!(f, "${}", i),
Type::Name(n) => write!(f, "{}", n),
Type::Generic(g) => write!(f, "'{}", g),

Type::Func(args, ret) => {
write!(f, "Fn[")?;

let mut first = true;
for arg in args {
if first {
write!(f, "{}", arg)?;
first = false;
} else {
write!(f, ", {}", arg)?;
}
}

write!(f, "] -> {}", ret)
}

Type::App(f_, a) => {
write!(f, "{}[", f_)?;
let mut first = true;
for a in a {
if first {
write!(f, "{}", a)?;
first = false;
} else {
write!(f, ", {}", a)?;
}
}

write!(f, "]")
}
}
}
}

#[derive(Debug)]
pub enum Expr {
Integer(u64),
Symbol(String),
String(String),
FuncCall {
func: Box<Expr>,
args: Vec<Expr>,
},
}

#[derive(Debug)]
pub enum Pattern {
Wildcard,
Symbol(String),
Variant {
name: String,
args: Vec<Pattern>,
exhaustive: bool,
},
Or(Vec<Pattern>),
}

#[derive(Debug)]
pub enum Statement {
FuncCall {
func: String,
args: Vec<Expr>,
},

Let {
name: String,
value: Expr,
},

Set {
name: String,
value: Expr,
},

Loop {
body: Vec<Statement>,
},

Break,
Continue,

Return(Option<Expr>),

If {
cond: Expr,
then: Vec<Statement>,
elsy: Vec<Statement>,
},

Match {
value: Expr,
branches: Vec<(Pattern, Vec<Statement>)>,
},
}

#[derive(Debug, Clone)]
pub struct Variant {
pub name: String,
pub fields: Vec<Type>,
}

#[derive(Debug)]
pub enum TopLevel {
TypeDef {
name: String,
generics: Vec<String>,
variants: Vec<Variant>,
},

FuncDef {
name: String,
args: Vec<(String, Type)>,
ret: Type,
stats: Vec<Statement>,
},
}
Loading

0 comments on commit e61236e

Please sign in to comment.