aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler.rs
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-01-18 20:35:06 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-01-18 20:35:06 +0100
commite84d99376ba669c41b38a8750f0b4411ac5d57dc (patch)
tree16f0bc0e2e32f1e16df9a7b4fab623a9439fd8b7 /src/compiler.rs
parentc7fa3245f4b73e0699ccfc3d359d864247f62dcc (diff)
downloadsylt-e84d99376ba669c41b38a8750f0b4411ac5d57dc.tar.gz
Typecheck all blocks
Diffstat (limited to 'src/compiler.rs')
-rw-r--r--src/compiler.rs24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 1a71482..1a6cfc6 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -121,6 +121,8 @@ struct Compiler {
panic: bool,
errors: Vec<Error>,
+
+ blocks: Vec<Rc<Block>>,
}
macro_rules! push_frame {
@@ -174,6 +176,8 @@ impl Compiler {
panic: false,
errors: vec![],
+
+ blocks: Vec::new(),
}
}
@@ -483,8 +487,10 @@ impl Compiler {
}
function_block.ty = Type::Function(args, Box::new(return_type));
+ let function_block = Rc::new(function_block);
- block.add(Op::Constant(Value::Function(Rc::new(function_block))), self.line());
+ block.add(Op::Constant(Value::Function(Rc::clone(&function_block))), self.line());
+ self.blocks.push(function_block);
}
fn variable_expression(&mut self, block: &mut Block) {
@@ -737,9 +743,15 @@ impl Compiler {
}
- pub fn compile(&mut self, name: &str, file: &Path) -> Result<Block, Vec<Error>> {
- let mut block = Block::new(name, file, 0);
+ pub fn compile(&mut self, name: &str, file: &Path) -> Result<Vec<Rc<Block>>, Vec<Error>> {
+ self.stack_mut().push(Variable {
+ name: String::from("/main/"),
+ typ: Type::Void,
+ scope: 0,
+ active: false,
+ });
+ let mut block = Block::new(name, file, 0);
while self.peek() != Token::EOF {
self.statement(&mut block);
expect!(self, Token::Newline | Token::EOF, "Expect newline or EOF after expression.");
@@ -748,14 +760,16 @@ impl Compiler {
block.add(Op::Return, self.line());
block.ty = Type::Function(Vec::new(), Box::new(Type::Void));
+ self.blocks.insert(0, Rc::new(block));
+
if self.errors.is_empty() {
- Ok(block)
+ Ok(self.blocks.clone())
} else {
Err(self.errors.clone())
}
}
}
-pub fn compile(name: &str, file: &Path, tokens: TokenStream) -> Result<Block, Vec<Error>> {
+pub fn compile(name: &str, file: &Path, tokens: TokenStream) -> Result<Vec<Rc<Block>>, Vec<Error>> {
Compiler::new(file, tokens).compile(name, file)
}