added rust ast

This commit is contained in:
2021-08-07 20:20:11 -06:00
parent e41c2973a4
commit 37d998d6b5
2 changed files with 186 additions and 70 deletions

View File

@@ -11,9 +11,9 @@ impl IdGenerator {
IdGenerator{counter: 0}
}
pub fn next(&mut self) -> i64 {
pub fn next(&mut self) -> String {
self.counter += 1;
self.counter
("S" + self.counter.to_string()).to_string()
}
}
@@ -67,7 +67,7 @@ pub struct Spanned<T> {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct FunctionTypeUsage {
pub arguments: Vec<TypeUsage>,
pub return_type: TypeUsage,
pub return_type: Box<TypeUsage>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -87,6 +87,38 @@ pub enum TypeUsage {
Unknown(UnknownTypeUsage),
}
impl TypeUsage {
pub fn new_unknown(id_gen: &mut IdGenerator) -> TypeUsage {
return TypeUsage::Unknown(UnknownTypeUsage{
name: id_gen.next(),
});
}
pub fn new_named(identifier: &Identifier) -> TypeUsage {
return TypeUsage::Named(NamedTypeUsage{
name: identifier.clone(),
});
}
pub fn new_builtin(name: String) -> TypeUsage {
ast::TypeUsage::Named(ast::NamedTypeUsage{
name: ast::Identifier{
name: ast::Spanned{
span: ast::Span{left: 0, right: 0}, //todo: figure out a sane value for these
value: name,
}
}
)
}
pub fn new_function(arg_count: usize, id_gen: &mut IdGenerator) -> TypeUsage {
return TypeUsage::Function(FunctionTypeUsage{
arguments: 0..arg_count.map(|_| => TypeUsage.new_unknown(&mut id_gen)).collect(),
return_type: Box::new(TypeUsage.new_unknown(&mut id_gen)),
});
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Operator {
Mul,
@@ -98,19 +130,20 @@ pub enum Operator {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralInt {
pub value: Spanned<i64>,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralFloat {
pub value: Spanned<f64>,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralStruct {
pub name: Identifier,
pub fields: HashMap<Identifier, Expression>,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -122,14 +155,14 @@ pub struct Identifier {
pub struct FunctionCall {
pub source: Expression,
pub arguments: Vec<Expression>,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct StructGetter {
pub source: Box<Expression>,
pub source: Expression,
pub attribute: Identifier,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -137,18 +170,20 @@ pub struct Operation {
pub left: Expression,
pub op: Operator,
pub right: Expression,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct VariableUsage {
pub name: Identifier,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Subexpression {
LiteralInt(LiteralInt),
LiteralFloat(LiteralFloat),
LiteralStruct(LiteralStruct),
FunctionCall(FunctionCall),
Identifier(Identifier),
Op(Operation),
@@ -157,20 +192,19 @@ pub enum Subexpression {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Expression {
pub subexpression: Spanned<Box<Subexpression>>,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ReturnStatement {
pub source: Expression,
pub type: TypeUsage,
}
};
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LetStatement {
variable_name: Identifier,
type: VariableUsage,
expression: Expression,
type_: TypeUsage,
}
pub enum AssignmentTarget {
@@ -182,25 +216,26 @@ pub enum AssignmentTarget {
pub struct AssignmentStatement {
pub source: AssignmentTarget,
pub expression: Expression,
pub type: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Statement {
Assignment(Assignment),
Return(ReturnStatement),
Let(LetStatement),
Assignment(AssignmentStatement),
Expression(Expression),
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Block {
pub statements: Vec<Spanned<Statement>>,
pub type: TypeUsage,
pub statements: Vec<Statement>,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct VariableDeclaration {
pub name: Identifier,
pub type: TypeUsage,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -221,10 +256,16 @@ pub struct PrimitiveTypeDeclaration {
pub name: String, // cannot be identifier as it's not declared anywhere specific, it's builtins
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct StructField {
pub name: Identifier,
pub type_: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct StructTypeDeclaration {
pub name: Identifier,
pub fields: HashMap<Identifier, TypeUsage>,
pub fields: Vec<StructField>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -241,14 +282,19 @@ pub enum TypeDeclaration {
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Impl {
pub struct Impl {
pub struct_name: Identifier,
pub functions: Vec<Function>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Module {
pub functions: Vec<Function>,
pub types: Vec<TypeDeclaration>,
pub impls: Vec<Impls>,
pub enum ModuleItem {
Function(Function),
TypeDeclaration(TypeDeclaration),
Impl(Impl),
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Module {
pub items: Vec<ModuleItem>,
}