working on conversion back to rust

This commit is contained in:
Andrew Segavac
2021-07-23 08:58:17 -06:00
parent 691009dd59
commit e41c2973a4
3 changed files with 203 additions and 38 deletions

View File

@@ -202,14 +202,6 @@ class Impl:
functions: List[Function] functions: List[Function]
@dataclass
class FunctionDeclartation:
name: str
arguments: List[VariableDeclaration]
return_type: TypeUsage
type: TypeUsage
TraitItem = Union[FunctionDeclaration, Function] TraitItem = Union[FunctionDeclaration, Function]

View File

@@ -1,6 +1,56 @@
use std::collections::HashMap;
use crate::types; use crate::types;
pub struct IdGenerator {
counter: i64,
}
impl IdGenerator {
pub fn new() -> Self {
IdGenerator{counter: 0}
}
pub fn next(&mut self) -> i64 {
self.counter += 1;
self.counter
}
}
pub fn new_named(name: String) -> {
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_unit() -> {
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: "()".to_string(),
}
}
)
}
pub fn new_never() -> {
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: "!".to_string(),
}
}
)
}
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Span { pub struct Span {
pub left: usize, pub left: usize,
@@ -13,12 +63,28 @@ pub struct Spanned<T> {
pub value: T, pub value: T,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct TypeUsage { pub struct FunctionTypeUsage {
pub name: Spanned<Identifier>, pub arguments: Vec<TypeUsage>,
pub ty: types::SpecifiedType, pub return_type: TypeUsage,
//TODO: Generics go here }
// mut, weak here
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NamedTypeUsage {
name: Identifier,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct UnknownTypeUsage {
name: String,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum TypeUsage {
Function(FunctionTypeUsage),
Named(NamedTypeUsage),
Unknown(UnknownTypeUsage),
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
@@ -31,70 +97,158 @@ pub enum Operator {
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralInt { pub struct LiteralInt {
pub value: i64, pub value: Spanned<i64>,
pub type: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralFloat {
pub value: Spanned<f64>,
pub type: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct LiteralStruct {
pub fields: HashMap<Identifier, Expression>,
pub type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Identifier { pub struct Identifier {
pub name: String, pub name: Spanned<String>,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct FunctionCall { pub struct FunctionCall {
pub name: Spanned<Identifier>, pub source: Expression,
pub arguments: Vec<Spanned<Box<Expression>>>, pub arguments: Vec<Expression>,
pub type: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct StructGetter {
pub source: Box<Expression>,
pub attribute: Identifier,
pub type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Operation { pub struct Operation {
pub left: Spanned<Box<Expression>>, pub left: Expression,
pub op: Operator, pub op: Operator,
pub right: Spanned<Box<Expression>>, pub right: Expression,
pub type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub enum Expression { pub struct VariableUsage {
LiteralInt(Spanned<LiteralInt>), pub name: Identifier,
FunctionCall(Spanned<FunctionCall>), pub type: TypeUsage,
Identifier(Spanned<Identifier>), }
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Subexpression {
LiteralInt(LiteralInt),
FunctionCall(FunctionCall),
Identifier(Identifier),
Op(Operation), Op(Operation),
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Assignment { pub struct Expression {
pub name: Spanned<Identifier>, pub subexpression: Spanned<Box<Subexpression>>,
pub type_usage: Option<Spanned<TypeUsage>>, pub type: TypeUsage,
// mut, weak here if type not used }
pub expression: Spanned<Box<Expression>>,
#[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,
}
pub enum AssignmentTarget {
Variable(VariableUsage),
StructAttr(StructGetter),
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct AssignmentStatement {
pub source: AssignmentTarget,
pub expression: Expression,
pub type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub enum Statement { pub enum Statement {
Assignment(Spanned<Assignment>), Assignment(Assignment),
Expression(Spanned<Box<Expression>>), Expression(Expression),
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Block { pub struct Block {
pub statements: Vec<Spanned<Statement>>, pub statements: Vec<Spanned<Statement>>,
pub type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct VariableDeclaration { pub struct VariableDeclaration {
pub name: Spanned<Identifier>, pub name: Identifier,
pub type_usage: Spanned<TypeUsage>, pub type: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct FunctionDeclaration {
pub name: Identifier,
pub arguments: Vec<VariableDeclaration>,
pub return_type: TypeUsage,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Function { pub struct Function {
pub name: Spanned<Identifier>, pub declaration: FunctionDeclaration,
pub arguments: Vec<VariableDeclaration>,
pub return_type: Spanned<TypeUsage>,
pub block: Block, pub block: Block,
} }
#[derive(Clone, PartialEq, Eq, Hash)]
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 StructTypeDeclaration {
pub name: Identifier,
pub fields: HashMap<Identifier, TypeUsage>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct AliasTypeDeclaration {
pub name: Identifier,
pub replaces: TypeUsage,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum TypeDeclaration {
Struct(StructTypeDeclaration),
Primitive(PrimitiveTypeDeclaration),
Alias(AliasTypeDeclaration),
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Impl {
pub struct_name: Identifier,
pub functions: Vec<Function>,
}
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct Module { pub struct Module {
pub functions: Vec<Function>, pub functions: Vec<Function>,
pub types: Vec<TypeDeclaration>,
pub impls: Vec<Impls>,
} }

View File

@@ -2,11 +2,30 @@ use std::str::FromStr;
use crate::ast; use crate::ast;
use crate::types; use crate::types;
grammar; grammar(id_generator: ast::IdGenerator);
pub LiteralInt: ast::LiteralInt = { pub TypeUsage: ast::TypeUsage = {
<literal:r"[0-9]+"> => ast::LiteralInt{value: i64::from_str(literal).unwrap()} "fn" "(" <args:Comma<TypeUsage>> ")" => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: ast::new_unit()}),
"fn" "(" <args:Comma<TypeUsage>> ")" ":" <return_type:TypeUsage> => ast::TypeUsage::Function(ast::FunctionTypeUsage{arguments: args, return_type: return_type),
<name:Identifier> => ast::TypeUsage::Named(ast::NamedTypeUsage{name: name})
};
pub LiteralInt: i64 = {
<literal:r"[0-9]+"> => i64::from_str(literal).unwrap()
};
pub SpannedLiteralInt: ast::LiteralInt {
<literal_int:Spanned<LiteralInt>> => ast::LiteralInt{value: literal_int, type: ast::new_named("i64")},
};
pub LiteralFloat: f64 = {
<literal:r"[0-9]+\.[0-9]+"> => f64::from_str(literal).unwrap()
};
pub SpannedLiteralInt: ast::LiteralFloat {
<literal_float:Spanned<LiteralFloat>> => ast::LiteralFloat{value: literal_float, type: ast::new_named("f64")},
}; };
pub Identifier: ast::Identifier = { pub Identifier: ast::Identifier = {