scheme_rs/
ast.rs

1use crate::{
2    env::Env,
3    eval::Eval,
4    expand::Transformer,
5    gc::Trace,
6    num::Number,
7    syntax::{Identifier, Mark, Span, Syntax},
8    util::ArcSlice,
9    value::Value,
10};
11use std::sync::Arc;
12
13#[derive(Debug, Clone, PartialEq, Trace)]
14pub struct ByteVector(pub Vec<u8>);
15
16#[derive(Debug, Clone, PartialEq, Trace)]
17pub enum Literal {
18    Number(Number),
19    Boolean(bool),
20    Character(char),
21    String(String),
22    ByteVector(ByteVector),
23}
24
25#[derive(Clone, Trace)]
26pub struct Quote {
27    pub val: Value,
28}
29
30#[derive(Clone, Trace)]
31pub struct SyntaxQuote {
32    pub syn: Syntax,
33    pub env: Env,
34}
35
36#[derive(Clone, Trace)]
37pub struct Call {
38    pub args: ArcSlice<Arc<dyn Eval>>,
39    pub location: Span,
40    pub proc_name: String,
41}
42
43#[derive(Clone, Trace)]
44pub struct DefineFunc {
45    pub name: Identifier,
46    pub args: Formals,
47    pub body: Body,
48}
49
50#[derive(Clone, Trace)]
51pub struct DefineVar {
52    pub name: Identifier,
53    pub val: Arc<dyn Eval>,
54}
55
56#[derive(Clone, Trace, derive_more::Debug)]
57pub enum Define {
58    DefineVar(#[debug(skip)] DefineVar),
59    DefineFunc(#[debug(skip)] DefineFunc),
60}
61
62#[derive(Clone, Trace)]
63pub struct DefineSyntax;
64
65#[derive(Clone, Trace)]
66pub struct Lambda {
67    pub args: Formals,
68    pub body: Body,
69}
70
71#[derive(Debug, Clone, Trace)]
72pub enum Formals {
73    FixedArgs(Vec<Identifier>),
74    VarArgs {
75        fixed: Vec<Identifier>,
76        remaining: Identifier,
77    },
78}
79
80impl Formals {
81    pub fn to_args_and_remaining(&self) -> (Vec<Identifier>, Option<Identifier>) {
82        match self {
83            Self::VarArgs { fixed, remaining } => (fixed.clone(), Some(remaining.clone())),
84            Self::FixedArgs(args) => (args.clone(), None),
85        }
86    }
87}
88
89#[derive(Clone, Trace)]
90pub struct Body {
91    pub exprs: ArcSlice<Arc<dyn Eval>>,
92}
93
94impl Body {
95    pub fn new(exprs: Vec<Arc<dyn Eval>>) -> Self {
96        Self {
97            exprs: ArcSlice::from(exprs),
98        }
99    }
100}
101
102#[derive(Clone, Trace)]
103pub struct Let {
104    pub bindings: Arc<[(Identifier, Arc<dyn Eval>)]>,
105    pub body: Body,
106}
107
108#[derive(Clone, Trace)]
109pub struct Set {
110    pub var: Identifier,
111    pub val: Arc<dyn Eval>,
112}
113
114#[derive(Clone, Trace)]
115pub struct If {
116    pub cond: Arc<dyn Eval>,
117    pub success: Arc<dyn Eval>,
118    pub failure: Option<Arc<dyn Eval>>,
119}
120
121#[derive(Clone, Trace)]
122pub struct And {
123    pub args: ArcSlice<Arc<dyn Eval>>,
124}
125
126impl And {
127    pub fn new(args: Vec<Arc<dyn Eval>>) -> Self {
128        Self {
129            args: ArcSlice::from(args),
130        }
131    }
132}
133
134#[derive(Clone, Trace)]
135pub struct Or {
136    pub args: ArcSlice<Arc<dyn Eval>>,
137}
138
139impl Or {
140    pub fn new(args: Vec<Arc<dyn Eval>>) -> Self {
141        Self {
142            args: ArcSlice::from(args),
143        }
144    }
145}
146
147#[derive(Clone, Trace)]
148pub struct Vector {
149    pub vals: Vec<Arc<dyn Eval>>,
150}
151
152#[derive(Clone, Trace)]
153pub struct SyntaxCase {
154    pub arg: Arc<dyn Eval>,
155    pub transformer: Transformer,
156}
157
158#[derive(Clone, Trace)]
159pub struct SyntaxRules {
160    pub transformer: Transformer,
161}
162
163#[derive(Clone, Trace)]
164pub struct Apply {
165    pub proc_name: String,
166    pub location: Span,
167    pub args: ArcSlice<Arc<dyn Eval>>,
168    pub rest_args: Arc<dyn Eval>,
169}
170
171#[derive(Clone, Trace)]
172pub struct FetchVar {
173    pub ident: Identifier,
174}
175
176impl FetchVar {
177    pub fn new(ident: Identifier) -> Self {
178        Self { ident }
179    }
180}
181
182#[derive(Clone, Trace)]
183pub struct MacroExpansionPoint {
184    pub mark: Mark,
185    pub macro_env: Env,
186    pub expr: Arc<dyn Eval>,
187}
188
189impl MacroExpansionPoint {
190    pub fn new(mark: Mark, macro_env: Env, expr: Arc<dyn Eval>) -> Self {
191        Self {
192            mark,
193            macro_env,
194            expr,
195        }
196    }
197}
198/*
199struct Export {}
200
201struct Import {}
202
203struct Library {
204    imports: Vec<Import>,
205    exports: Vec<Export>,
206    library_body: Body,
207}
208
209struct TopLevelProgram {
210    imports: Vec<Import>,
211    program_body: Body,
212}
213*/