working on conversion back to rust
This commit is contained in:
@@ -202,14 +202,6 @@ class Impl:
|
||||
functions: List[Function]
|
||||
|
||||
|
||||
@dataclass
|
||||
class FunctionDeclartation:
|
||||
name: str
|
||||
arguments: List[VariableDeclaration]
|
||||
return_type: TypeUsage
|
||||
type: TypeUsage
|
||||
|
||||
|
||||
TraitItem = Union[FunctionDeclaration, Function]
|
||||
|
||||
|
||||
|
||||
208
src/ast.rs
208
src/ast.rs
@@ -1,6 +1,56 @@
|
||||
use std::collections::HashMap;
|
||||
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)]
|
||||
pub struct Span {
|
||||
pub left: usize,
|
||||
@@ -13,12 +63,28 @@ pub struct Spanned<T> {
|
||||
pub value: T,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct TypeUsage {
|
||||
pub name: Spanned<Identifier>,
|
||||
pub ty: types::SpecifiedType,
|
||||
//TODO: Generics go here
|
||||
// mut, weak here
|
||||
pub struct FunctionTypeUsage {
|
||||
pub arguments: Vec<TypeUsage>,
|
||||
pub return_type: TypeUsage,
|
||||
}
|
||||
|
||||
#[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)]
|
||||
@@ -31,70 +97,158 @@ pub enum Operator {
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
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)]
|
||||
pub struct Identifier {
|
||||
pub name: String,
|
||||
pub name: Spanned<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct FunctionCall {
|
||||
pub name: Spanned<Identifier>,
|
||||
pub arguments: Vec<Spanned<Box<Expression>>>,
|
||||
pub source: 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)]
|
||||
pub struct Operation {
|
||||
pub left: Spanned<Box<Expression>>,
|
||||
pub left: Expression,
|
||||
pub op: Operator,
|
||||
pub right: Spanned<Box<Expression>>,
|
||||
pub right: Expression,
|
||||
pub type: TypeUsage,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Expression {
|
||||
LiteralInt(Spanned<LiteralInt>),
|
||||
FunctionCall(Spanned<FunctionCall>),
|
||||
Identifier(Spanned<Identifier>),
|
||||
pub struct VariableUsage {
|
||||
pub name: Identifier,
|
||||
pub type: TypeUsage,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Subexpression {
|
||||
LiteralInt(LiteralInt),
|
||||
FunctionCall(FunctionCall),
|
||||
Identifier(Identifier),
|
||||
Op(Operation),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Assignment {
|
||||
pub name: Spanned<Identifier>,
|
||||
pub type_usage: Option<Spanned<TypeUsage>>,
|
||||
// mut, weak here if type not used
|
||||
pub expression: Spanned<Box<Expression>>,
|
||||
pub struct Expression {
|
||||
pub subexpression: Spanned<Box<Subexpression>>,
|
||||
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,
|
||||
}
|
||||
|
||||
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)]
|
||||
pub enum Statement {
|
||||
Assignment(Spanned<Assignment>),
|
||||
Expression(Spanned<Box<Expression>>),
|
||||
Assignment(Assignment),
|
||||
Expression(Expression),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Block {
|
||||
pub statements: Vec<Spanned<Statement>>,
|
||||
pub type: TypeUsage,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct VariableDeclaration {
|
||||
pub name: Spanned<Identifier>,
|
||||
pub type_usage: Spanned<TypeUsage>,
|
||||
pub name: Identifier,
|
||||
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)]
|
||||
pub struct Function {
|
||||
pub name: Spanned<Identifier>,
|
||||
pub arguments: Vec<VariableDeclaration>,
|
||||
pub return_type: Spanned<TypeUsage>,
|
||||
pub declaration: FunctionDeclaration,
|
||||
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)]
|
||||
pub struct Module {
|
||||
pub functions: Vec<Function>,
|
||||
pub types: Vec<TypeDeclaration>,
|
||||
pub impls: Vec<Impls>,
|
||||
}
|
||||
|
||||
@@ -2,11 +2,30 @@ use std::str::FromStr;
|
||||
use crate::ast;
|
||||
use crate::types;
|
||||
|
||||
grammar;
|
||||
grammar(id_generator: ast::IdGenerator);
|
||||
|
||||
|
||||
pub LiteralInt: ast::LiteralInt = {
|
||||
<literal:r"[0-9]+"> => ast::LiteralInt{value: i64::from_str(literal).unwrap()}
|
||||
pub TypeUsage: ast::TypeUsage = {
|
||||
"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 = {
|
||||
|
||||
Reference in New Issue
Block a user