11// Typesharp ast
22// use crate::{ typesharp_parser::Module };
33use super :: position:: { Position } ;
4+ use super :: op:: * ;
5+ use super :: types;
6+ use crate :: { compiler:: typesharp_lexer:: token:: Token } ;
47
58pub struct AST {
6- // name
7- n : String ,
8- // // program type
9- // t: ProgramType,
10- // // Stack constants
11- // s: Vec<ASTExpr>,
12- // // Body
13- // b: Vec<ASTNode>
9+ name : String ,
10+ typ : ProgramType ,
11+ body : Vec < ASTStatement >
1412}
1513
16- pub trait AnyContext {
17- fn new ( & self ) -> Self ;
18- fn getLine ( ) -> u8 ;
14+ #[ derive( Clone , PartialEq , Debug ) ]
15+ pub struct AnyContext ;
16+
17+ impl AnyContext {
18+ fn new ( & self ) -> AnyContext {
19+ return Self ;
20+ }
21+
22+ fn getLine ( ) -> u8 {
23+ 0
24+ }
1925}
2026
2127pub enum ProgramType {
@@ -33,13 +39,65 @@ pub enum ProgramType {
3339
3440pub struct Library ;
3541
36- pub struct Statement < Context > {
37- pub body : StmtBody ,
38- pub context : Context ,
42+ #[ derive( Clone , PartialEq , Debug ) ]
43+ pub struct ASTStatement {
44+ pub body : ASTStateBody ,
45+ pub context : AnyContext ,
3946 pub pos : Position
4047}
4148
4249#[ derive( Clone , PartialEq , Debug ) ]
43- pub enum StmtBody {
50+ pub enum ASTStateBody {
4451 // expressions, function calls, returns etc should be here.
52+ FuncCall ( Signature ) ,
53+ Expression ( Expression ) ,
54+ StackVar ( Var ) ,
55+ Constant ( HeapVar ) ,
56+ AnyConstant ( AnyVar ) ,
57+ If ( Conditional )
58+ }
59+
60+ // Context and definitions
61+ /// A variable, const, class, etc.
62+ #[ derive( Clone , PartialEq , Debug ) ]
63+ pub struct Var {
64+ pub op : Option < AnyOp > ,
65+ pub typ : Option < types:: Type > ,
66+ pub val : Token ,
67+ pub pos : Position ,
68+ pub dies : bool ,
69+ // pub typ: Type
70+ }
71+
72+ /// Dynamic variable, extends var, which is static.
73+ #[ derive( Clone , PartialEq , Debug ) ]
74+ pub struct HeapVar {
75+ pub var : Var ,
76+ pub mangled : bool
77+ }
78+
79+ #[ derive( Clone , PartialEq , Debug ) ]
80+ pub enum AnyVar {
81+ Static ( Var ) ,
82+ Heap ( HeapVar )
83+ }
84+
85+ /// Functions
86+ #[ derive( Clone , PartialEq , Debug ) ]
87+ pub struct Signature {
88+ pub name : Var ,
89+ pub dynamic : bool
90+ }
91+
92+ #[ derive( Clone , PartialEq , Debug ) ]
93+ pub struct Expression {
94+ pub ops : Vec < AnyOp > ,
95+ pub v : Vec < AnyVar >
96+ }
97+
98+ #[ derive( Clone , PartialEq , Debug ) ]
99+ pub struct Conditional {
100+ pub condition : Expression ,
101+ pub body : Vec < ASTStatement > ,
102+ pub fin : Option < Vec < ASTStatement > >
45103}
0 commit comments