Skip to main content

tsh/
ir.rs

1//! Target-independent, typed language semantics.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum Type {
4    String,
5    Number,
6    Boolean,
7    Void,
8    Class(String),
9}
10#[derive(Debug, Clone)]
11pub struct Expr {
12    pub kind: ExprKind,
13    pub ty: Type,
14}
15#[derive(Debug, Clone)]
16pub enum ExprKind {
17    String(String),
18    Number(i64),
19    Boolean(bool),
20    Variable(String),
21    Template(Vec<Expr>),
22    Binary {
23        op: String,
24        left: Box<Expr>,
25        right: Box<Expr>,
26    },
27    Unary {
28        op: String,
29        value: Box<Expr>,
30    },
31    Call {
32        function: String,
33        args: Vec<Expr>,
34    },
35    New {
36        class: String,
37        args: Vec<Expr>,
38    },
39    Field {
40        object: Box<Expr>,
41        class: String,
42        field: String,
43    },
44    Method {
45        object: Option<Box<Expr>>,
46        class: String,
47        method: String,
48        args: Vec<Expr>,
49    },
50    Env(Box<Expr>),
51}
52#[derive(Debug, Clone)]
53pub enum Place {
54    Variable(String),
55    Field {
56        object: Expr,
57        class: String,
58        field: String,
59    },
60}
61#[derive(Debug, Clone)]
62pub enum Stmt {
63    Let {
64        name: String,
65        value: Expr,
66    },
67    Assign {
68        place: Place,
69        value: Expr,
70    },
71    Expr(Expr),
72    If {
73        condition: Expr,
74        then_body: Vec<Stmt>,
75        else_body: Vec<Stmt>,
76    },
77    While {
78        condition: Expr,
79        body: Vec<Stmt>,
80    },
81    For {
82        init: Vec<Stmt>,
83        condition: Option<Expr>,
84        update: Vec<Stmt>,
85        body: Vec<Stmt>,
86    },
87    Return(Option<Expr>),
88    Break,
89    Continue,
90    Command {
91        kind: Command,
92        args: Vec<Expr>,
93        recursive: bool,
94    },
95    Raw(String),
96    Comment(String),
97    Block(Vec<Stmt>),
98}
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum Command {
101    Echo,
102    Mkdir,
103    Rm,
104    Cp,
105    Mv,
106    Cd,
107    Run,
108    Exit,
109    Chmod,
110}
111#[derive(Debug, Clone)]
112pub struct Param {
113    pub name: String,
114    pub ty: Type,
115}
116#[derive(Debug, Clone)]
117pub struct Function {
118    pub name: String,
119    pub params: Vec<Param>,
120    pub return_type: Type,
121    pub body: Vec<Stmt>,
122}
123#[derive(Debug, Clone)]
124pub struct Field {
125    pub name: String,
126    pub ty: Type,
127    pub initial: Option<Expr>,
128}
129#[derive(Debug, Clone)]
130pub struct Method {
131    pub function: Function,
132    pub is_static: bool,
133}
134#[derive(Debug, Clone)]
135pub struct Class {
136    pub name: String,
137    pub fields: Vec<Field>,
138    pub constructor: Function,
139    pub methods: Vec<Method>,
140}
141#[derive(Debug, Clone, Default)]
142pub struct Program {
143    pub functions: Vec<Function>,
144    pub classes: Vec<Class>,
145    pub body: Vec<Stmt>,
146}