Skip to content

Commit abda47c

Browse files
authored
πŸš€ feat: New AST (Abstract Syntax Tree)
See https://github.com/TypeSharp/Typesharp/pull/14
2 parents c110dad + d25a168 commit abda47c

File tree

14 files changed

+124
-35
lines changed

14 files changed

+124
-35
lines changed

β€Ž.gitignoreβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target
22
*-h.*
33
*_h.*
4-
src/error/*
4+
src/error/*
5+
keywords.yaml

β€ŽCargo.lockβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.tomlβ€Ž

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
[package]
2-
name = "Typesharp"
2+
name = "typesharp"
33
version = "0.1.0"
44
authors = ["John Bergman <[email protected]>"]
55
edition = "2018"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
9-
10-
#[lib]
11-
#name = "typesharp"
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// Typesharp ast
22
// use crate::{ typesharp_parser::Module };
33
use super::position::{ Position };
4+
use super::op::*;
5+
use super::types;
6+
use crate::{ compiler::typesharp_lexer::token::Token };
47

58
pub 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

2127
pub enum ProgramType {
@@ -33,13 +39,65 @@ pub enum ProgramType {
3339

3440
pub 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
}

β€Žsrc/compiler/typesharp_ast/keyword.rsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{error, fmt};
22

33
/// These keywords are reserved.
44
/// If you use these as identifiers you will be yelled at.
5-
#[derive(Debug)]
5+
#[derive(Clone, PartialEq, Debug)]
66
pub enum KeyWord {
77
/// Keywords available in all versions of typesharp.
88
/// Asm, allows use for inline assembly

β€Žsrc/compiler/typesharp_ast/mod.rsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod position;
33
pub mod keyword;
44
pub mod cursor;
55
pub mod op;
6+
pub mod types;
67

78
pub use self::{
89
position::{ Position, Span },

β€Žsrc/compiler/typesharp_ast/op.rsβ€Ž

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
#[derive(Clone, PartialEq, Debug)]
2+
pub enum AnyOp {
3+
BinOp(BinOp),
4+
UnaryOp(UnaryOp),
5+
LogicalOp(LogicalOp),
6+
ComparisonOp(ComparisonOp),
7+
AssignmentOp(AssignmentOp)
8+
}
9+
110
// Binary Operators
2-
#[derive(Debug)]
11+
#[derive(Clone, PartialEq, Debug)]
312
pub enum BinOp {
413
// +
514
Plus,
@@ -35,7 +44,7 @@ pub enum BinOp {
3544
UShr,
3645
}
3746

38-
#[derive(Debug)]
47+
#[derive(Clone, PartialEq, Debug)]
3948
pub enum UnaryOp {
4049
// ++x
4150
IncP,
@@ -65,7 +74,7 @@ pub enum UnaryOp {
6574
Object,
6675
}
6776

68-
#[derive(Debug)]
77+
#[derive(Clone, PartialEq, Debug)]
6978
pub enum LogicalOp {
7079
// x && y
7180
And,
@@ -77,7 +86,7 @@ pub enum LogicalOp {
7786
Coalasce,
7887
}
7988

80-
#[derive(Debug)]
89+
#[derive(Clone, PartialEq, Debug)]
8190
pub enum ComparisonOp {
8291
Eq,
8392

@@ -98,7 +107,7 @@ pub enum ComparisonOp {
98107
InstanceOf,
99108
}
100109

101-
#[derive(Debug)]
110+
#[derive(Clone, PartialEq, Debug)]
102111
pub enum AssignmentOp {
103112
// x += y
104113
Add,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this is used by the parser
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this is used by the parser
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
/// Types module
1+
// This is a module that handles internal classes and types.
2+
pub mod builtin;
3+
pub mod compiler;
4+
use super::{ ast };
5+
6+
#[derive(Clone, PartialEq, Debug)]
7+
pub struct Type {
8+
pub name: String,
9+
pub statement: TypeDefinition
10+
}
11+
12+
#[derive(Clone, PartialEq, Debug)]
13+
pub struct TypeDefinition {
14+
/// Used for linting and more descriptive errors
15+
pub calculated_defs: Option<Vec<ast::Conditional>>,
16+
pub defs: Option<Vec<ast::AnyVar>>
17+
}
18+
19+
pub use self::{
20+
builtin::*,
21+
compiler::*
22+
};

0 commit comments

Comments
Β (0)