diff --git a/Dockerfile b/Dockerfile index 03a7406..a3f234f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ ENV DEBIAN_FRONTEND=noninteractive ENV TZ=Etc/UTC RUN apt-get update -RUN apt-get install -y git valgrind gcc +RUN apt-get install -y git valgrind gcc python3 ENTRYPOINT git clone https://github.com/ocen-lang/ocen /ocen/ \ && cd /ocen/ \ @@ -21,4 +21,4 @@ ENV PATH=$PATH:/ocen/bootstrap/ ## Build the image # docker build . --tag tide ## Run the image -# docker run --rm -it -v $(pwd):/mnt/ -w /mnt/ tide \ No newline at end of file +# docker run --rm -it -v $(pwd):/mnt/ -w /mnt/ tide diff --git a/compiler/compiler.oc b/compiler/compiler.oc index b3ee33f..3687225 100644 --- a/compiler/compiler.oc +++ b/compiler/compiler.oc @@ -1,6 +1,7 @@ import std::vector::{ Vector } import std::span::{ Span } import std::sv::{ SV } +import std::mem import @ast::nodes::{ this, AST, Variable, Symbol } import @errors::{ Error } @@ -37,17 +38,19 @@ enum CompileType { Method } -def Compiler::make( +def Compiler::new( vm: &VM, name: SV, span: Span, type: CompileType, enclosing: &Compiler = null -): Compiler { +): &Compiler { + let compiler = mem::alloc() + let chunk = Chunk::new(span) let locals = Vector::new(capacity: 8) let upvars = Vector::new(capacity: 256) let func = gc::allocate_object(FunctionCode, vm, enclosing) - let compiler = Compiler( + *compiler = Compiler( vm, func, chunk, @@ -56,7 +59,7 @@ def Compiler::make( scope_depth: 0, enclosing ) - gc::set_compiler(&compiler) + gc::set_compiler(compiler) let name_val = vm.copy_string(name.data, name.len) func.init(name_val.as_string(), chunk, 0) // Replace arity later... @@ -73,6 +76,7 @@ def Compiler::make( def Compiler::free(&this) { .locals.free() .upvars.free() + mem::free(this) } def Compiler::make_str(&this, text: SV): Value { @@ -366,7 +370,7 @@ def Compiler::compile_expression(&this, node: &AST) { def Compiler::compile_function(&this, node: &AST, func: &nodes::Function) { let compile_type: CompileType = if func.is_method then Method else Function - let cc = Compiler::make( + let cc = Compiler::new( .vm, func.sym.name, func.sym.span, @@ -520,10 +524,11 @@ def Compiler::compile_ns(&this, root: &AST) { // TODO: More complex structures than just expressions? def compile_program(vm: &VM, root: &AST): &FunctionCode { - let compiler = Compiler::make(vm, SV("",0), root.span, Script) + let compiler = Compiler::new(vm, SV("",0), root.span, Script) compiler.compile_ns(root) compiler.chunk.push_op(Halt, root.span) gc::set_compiler(null) + let func = compiler.func compiler.free() - return compiler.func + return func } diff --git a/tests/class_methods.td b/tests/class_methods.td new file mode 100644 index 0000000..94b4590 --- /dev/null +++ b/tests/class_methods.td @@ -0,0 +1,11 @@ +/// out: 5 8 + +class Foo { + def bar() => .x = 5 + def test(a, b) => .x + a + b +} + +let f = Foo() +print(f) +print(f.bar()) +print(f.test(1, 2)) \ No newline at end of file diff --git a/tests/instance_field_access.td b/tests/instance_field_access.td new file mode 100644 index 0000000..5e1eb58 --- /dev/null +++ b/tests/instance_field_access.td @@ -0,0 +1,7 @@ +/// out: 10 hello + +class Foo {} +let x = Foo() +x.bar = 10 +x.baz = "hello" +print(x.bar, x.baz) \ No newline at end of file