mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-14 09:25:02 +01:00
fix return values
This commit is contained in:
parent
eaa2f3527c
commit
b063850b53
8 changed files with 64 additions and 15 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,4 +1,6 @@
|
|||
/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
*.iml
|
||||
|
||||
*.snap.new
|
||||
|
|
@ -109,6 +109,7 @@ pub fn compile<'ast, 'bc, 'gc>(
|
|||
impl<'bc, 'gc> Compiler<'bc, 'gc> {
|
||||
fn compile(&mut self, ast: &Program) -> CResult {
|
||||
let global_block = FnBlock {
|
||||
name: self.rt.intern_string("<main>"),
|
||||
code: Vec::new_in(self.bump),
|
||||
stack_sizes: Vec::new_in(self.bump),
|
||||
spans: Vec::new_in(self.bump),
|
||||
|
|
@ -188,6 +189,7 @@ impl<'bc, 'gc> Compiler<'bc, 'gc> {
|
|||
|
||||
fn compile_fn_decl(&mut self, decl: &FnDecl) -> CResult {
|
||||
let block = FnBlock {
|
||||
name: decl.name.sym,
|
||||
code: Vec::new_in(self.bump),
|
||||
stack_sizes: Vec::new_in(self.bump),
|
||||
spans: Vec::new_in(self.bump),
|
||||
|
|
@ -480,7 +482,14 @@ impl<'bc, 'gc> Compiler<'bc, 'gc> {
|
|||
}
|
||||
|
||||
self.push_instr(Instr::Load(offset), StackChange::Grow, call.span);
|
||||
self.push_instr(Instr::Call, StackChange::None, call.span);
|
||||
// The callee gets rid of the params. We also pushed the load for the function above,
|
||||
// but the callee also leaves behind a return value.
|
||||
let expected_stack_shrink = params.len();
|
||||
self.push_instr(
|
||||
Instr::Call,
|
||||
StackChange::ShrinkN(expected_stack_shrink),
|
||||
call.span,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,10 +29,14 @@ use std::fmt::{Debug, Formatter};
|
|||
|
||||
use bumpalo::collections::Vec;
|
||||
|
||||
use crate::{errors::Span, runtime::vm::Value};
|
||||
use crate::{
|
||||
errors::Span,
|
||||
runtime::{gc::Symbol, vm::Value},
|
||||
};
|
||||
|
||||
/// This struct contains all data for a function.
|
||||
pub struct FnBlock<'bc> {
|
||||
pub name: Symbol,
|
||||
/// The bytecode of the function
|
||||
pub code: Vec<'bc, Instr>,
|
||||
/// The sizes of the stack required by the function after the instruction at the same index.
|
||||
|
|
@ -111,6 +115,7 @@ pub enum Instr {
|
|||
impl dbg_pls::DebugPls for FnBlock<'_> {
|
||||
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
|
||||
f.debug_struct("FnBlock")
|
||||
.field("name", &self.name)
|
||||
.field("arity", &self.arity)
|
||||
.field("code", &self.code.as_slice())
|
||||
.field("stack_sizes", &self.stack_sizes.as_slice())
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ impl<'s> Frame<'s> {
|
|||
vm_state.stack.push(Value::NativeU(old_pc));
|
||||
vm_state.stack.push(Value::Function(old_fn_block));
|
||||
|
||||
let frame_slice = &vm_state.stack[new_frame_offset..];
|
||||
// let frame_slice = &vm_state.stack[new_frame_offset..];
|
||||
|
||||
new_frame_offset
|
||||
}
|
||||
|
|
|
|||
21
test.dil
21
test.dil
|
|
@ -1,12 +1,15 @@
|
|||
fn test() {
|
||||
let a = 5;
|
||||
fn cooler_add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test();
|
||||
test();
|
||||
|
||||
let i = 0;
|
||||
while i < 100_000 {
|
||||
test();
|
||||
i = i + 1;
|
||||
fn add(a, b) {
|
||||
return cooler_add(a, b);
|
||||
}
|
||||
|
||||
let added = add(1, 5);
|
||||
|
||||
if added == 6 {
|
||||
print "correct";
|
||||
} else {
|
||||
print "FAILED";
|
||||
}
|
||||
|
|
@ -43,7 +43,6 @@ print "correct3";
|
|||
);
|
||||
|
||||
run_test!(
|
||||
#[ignore]
|
||||
parameters,
|
||||
r#"
|
||||
fn fancy_print(str) {
|
||||
|
|
@ -67,7 +66,6 @@ print x;
|
|||
);
|
||||
|
||||
run_test!(
|
||||
#[ignore]
|
||||
parameters_and_return,
|
||||
r#"
|
||||
fn add(a, b) {
|
||||
|
|
@ -84,6 +82,28 @@ if added == 6 {
|
|||
"#
|
||||
);
|
||||
|
||||
run_test!(
|
||||
#[ignore]
|
||||
nested_calls,
|
||||
r#"
|
||||
fn cooler_add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
fn add(a, b) {
|
||||
return cooler_add(a, b);
|
||||
}
|
||||
|
||||
let added = add(1, 5);
|
||||
|
||||
if added == 6 {
|
||||
print "correct";
|
||||
} else {
|
||||
print "FAILED";
|
||||
}
|
||||
"#
|
||||
);
|
||||
|
||||
run_test!(
|
||||
#[ignore]
|
||||
fib5,
|
||||
|
|
|
|||
5
tests/snapshots/functions__parameters.snap
Normal file
5
tests/snapshots/functions__parameters.snap
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/functions.rs
|
||||
expression: output
|
||||
---
|
||||
"correct\n"
|
||||
5
tests/snapshots/functions__parameters_and_return.snap
Normal file
5
tests/snapshots/functions__parameters_and_return.snap
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/functions.rs
|
||||
expression: output
|
||||
---
|
||||
"correct\n"
|
||||
Loading…
Add table
Add a link
Reference in a new issue