Skip to content

Commit

Permalink
Remove unused heap objects, improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
benson1029 committed Apr 12, 2024
1 parent 724d509 commit 9d60129
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 207 deletions.
23 changes: 0 additions & 23 deletions src/go/ece/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ function load(
preprocess_program(program, imports, true);
sort_global_declarations(program, imports, false);

tag_struct_methods(program);

// // Stub for loading the main function directly:
// let main = program.body.filter((x: any) => x.tag === "function" && x.name === "main")[0];
// const _main_addr = heap.allocate_any(main.body.body);
Expand Down Expand Up @@ -86,25 +84,4 @@ function load(
}
}

function tag_struct_methods(program: any) {
let struct_methods = {};
for (let stmt of program.body) {
if (stmt.tag === "struct-method") {
struct_methods[stmt.struct.name] = struct_methods[stmt.struct.name] || [];
struct_methods[stmt.struct.name].push(stmt);
}
}
for (let stmt of program.body) {
if (stmt.tag === "struct") {
if (!struct_methods[stmt.name]) continue;
for (let method of struct_methods[stmt.name]) {
stmt.fields.push({
name: method.name,
type: { tag: "method-type" }
})
}
}
}
}

export { load };
24 changes: 24 additions & 0 deletions src/go/ece/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,28 @@ describe('Channel', () => {
const result = evaluateFunctions(functions);
expect(result).toBe("42\n");
})

it("can evaluate channel receive statements", () => {
const functions = `
func main() {
ch := make(chan int32)
go func() {
ch <- 42
}()
<-ch
}
`
const result = evaluateFunctions(functions);
expect(result).toBe("");
})

it("can evaluate channel receive statements (blocked)", () => {
const functions = `
func main() {
ch := make(chan int32)
<-ch
}
`
expect(() => evaluateFunctions(functions)).toThrow();
})
})
22 changes: 22 additions & 0 deletions src/go/ece/tests/struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,26 @@ describe("Structs", () => {
const result = evaluateFunctions(functions);
expect(result).toBe("1\n2.5\n");
})

it("supports default values", () => {
const functions = `
type S struct {
x int32
y float32
z string
a bool
f func (int32) int32
}
func main() {
var s S
fmt.Println(s.x)
fmt.Println(s.y)
fmt.Println(s.z)
fmt.Println(s.a)
}
`
const result = evaluateFunctions(functions);
expect(result).toBe("0\n0\n\nfalse\n");
})
})
70 changes: 70 additions & 0 deletions src/go/ece/tests/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { parse } from '../../parser/go';
import { ECE } from '../ece';

function evaluateSequence(sequence) {
const program = `
package main
import "fmt"
func main() {
${sequence}
}
`
const parsed_program = parse(program);
const heapSize = 32768;
const return_value = (new ECE(heapSize, parsed_program)).evaluate(true).output;
const check_mark_and_sweep = (new ECE(heapSize, parsed_program)).evaluate(true, true).output;
expect(return_value).toBe(check_mark_and_sweep);
return return_value;
}

describe("Support boolean types", () => {
it('can create a boolean', () => {
const sequence = `
var b bool
b = true
fmt.Println(b)
`
const result = evaluateSequence(sequence);
expect(result).toBe("true\n");
})

it('can perform boolean operations', () => {
const sequence = `
var b bool
b = true
fmt.Println(!b)
c := b && true
fmt.Println(c)
d := b || false
fmt.Println(d)
`
const result = evaluateSequence(sequence);
expect(result).toBe("false\ntrue\ntrue\n");
})
})

describe("Support string types", () => {
it('can create a string', () => {
const sequence = `
var s string
s = "hello"
fmt.Println(s)
`
const result = evaluateSequence(sequence);
expect(result).toBe("hello\n");
})

it('can perform string operations', () => {
const sequence = `
var s string
s = "hello"
fmt.Println(s + " world")
t := s + " world"
fmt.Println(t)
`
const result = evaluateSequence(sequence);
expect(result).toBe("hello world\nhello world\n");
})
})
49 changes: 0 additions & 49 deletions src/go/heap/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { BuddyAllocator, WORD_SIZE } from "./alloc";
import { auto_cast } from "./types/auto_cast";
import { ComplexBuiltin } from "./types/complex/builtin";
import { ComplexLinkedList } from "./types/complex/linked_list";
import { ComplexPointer } from "./types/complex/pointer";
import { ComplexString } from "./types/complex/string";
import { ControlIndex } from "./types/control";
import { ControlAssign } from "./types/control/assign";
Expand Down Expand Up @@ -85,7 +84,6 @@ import { ControlMethodMember } from "./types/control/method_member";
import { ControlName } from "./types/control/name";
import { ControlNameAddress } from "./types/control/name_address";
import { ControlPopI } from "./types/control/pop_i";
import { ControlPostfix } from "./types/control/postfix";
import { ControlPushI } from "./types/control/push_i";
import { ControlRestoreEnvI } from "./types/control/restore_env_i";
import { ControlReturn } from "./types/control/return";
Expand Down Expand Up @@ -117,13 +115,11 @@ import {
TAGSTRING_PRIMITIVE_rune,
TAGSTRING_COMPLEX_string,
TAGSTRING_COMPLEX_linked_list,
TAGSTRING_COMPLEX_pointer,
TAGSTRING_CONTROL_name,
TAGSTRING_CONTROL_literal,
TAGSTRING_CONTROL_var,
TAGSTRING_CONTROL_assign,
TAGSTRING_CONTROL_unary,
TAGSTRING_CONTROL_postfix,
TAGSTRING_CONTROL_binary,
TAGSTRING_CONTROL_sequence,
TAGSTRING_CONTROL_call,
Expand Down Expand Up @@ -175,7 +171,6 @@ import {
TAGSTRING_USER_type_channel,
TAGSTRING_USER_type_struct_decl,
TAGSTRING_CONTROL_struct,
TAGSTRING_USER_type_method,
TAGSTRING_CONTROL_method,
TAGSTRING_CONTROL_method_member,
TAGSTRING_CONTROL_member_address_i,
Expand Down Expand Up @@ -204,7 +199,6 @@ import { UserTypeChannel } from "./types/user/type/channel";
import { UserTypeFloat32 } from "./types/user/type/float32";
import { UserTypeFunction } from "./types/user/type/function";
import { UserTypeInt32 } from "./types/user/type/int32";
import { UserTypeMethod } from "./types/user/type/method";
import { UserTypeMutex } from "./types/user/type/mutex";
import { UserTypeSlice } from "./types/user/type/slice";
import { UserTypeString } from "./types/user/type/string";
Expand Down Expand Up @@ -560,18 +554,6 @@ class Heap {
return ComplexLinkedList.allocate_from_array(this, values);
}

/**
* COMPLEX_pointer
* Fields : number of children
* Children : address of the object referenced
*
* @param value address value
* @returns address of the object
*/
public allocate_COMPLEX_pointer(value: number): number {
return ComplexPointer.allocate(this, value);
}

/**
* COMPLEX_builtin
* Fields : number of children
Expand Down Expand Up @@ -648,20 +630,6 @@ class Heap {
return ControlUnary.allocate(this, obj.operator, obj.operand);
}

/**
* CONTROL_postfix
* Fields : number of children
* Children :
* - 4 bytes address of the operator (COMPLEX_string)
* - 4 bytes address of the operand (expression)
*
* @param obj control object
* @returns address of the object
*/
public allocate_CONTROL_postfix(obj: { tag: string, operator: string, operand: any }): number {
return ControlPostfix.allocate(this, obj.operator, obj.operand);
}

/**
* CONTROL_binary
* Fields : number of children
Expand Down Expand Up @@ -1383,17 +1351,6 @@ class Heap {
return UserTypeStruct.allocate_load(this, obj.name, obj.members);
}

/**
* USER_type_method
* Fields :
* - number of children
* Children :
* - 4 bytes address of the name (COMPLEX_string)
*/
public allocate_USER_type_method(obj: { tag: string }): number {
return UserTypeMethod.allocate(this);
}

/**
* USER_type_mutex
* Fields :
Expand Down Expand Up @@ -1461,8 +1418,6 @@ class Heap {
return this.allocate_COMPLEX_string(obj.value);
case TAGSTRING_COMPLEX_linked_list:
return this.allocate_COMPLEX_linked_list(obj);
case TAGSTRING_COMPLEX_pointer:
return this.allocate_COMPLEX_pointer(obj);
case TAGSTRING_COMPLEX_builtin:
return this.allocate_COMPLEX_builtin(obj);
case TAGSTRING_CONTROL_name:
Expand All @@ -1475,8 +1430,6 @@ class Heap {
return this.allocate_CONTROL_assign(obj);
case TAGSTRING_CONTROL_unary:
return this.allocate_CONTROL_unary(obj);
case TAGSTRING_CONTROL_postfix:
return this.allocate_CONTROL_postfix(obj);
case TAGSTRING_CONTROL_binary:
return this.allocate_CONTROL_binary(obj);
case TAGSTRING_CONTROL_sequence:
Expand Down Expand Up @@ -1607,8 +1560,6 @@ class Heap {
return this.allocate_USER_type_struct_decl(obj);
case TAGSTRING_USER_type_struct:
return this.allocate_USER_type_struct(obj);
case TAGSTRING_USER_type_method:
return this.allocate_USER_type_method(obj);
case TAGSTRING_USER_type_mutex:
return this.allocate_USER_type_mutex();
case TAGSTRING_USER_type_slice:
Expand Down
12 changes: 0 additions & 12 deletions src/go/heap/types/auto_cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ComplexFunction } from "./complex/function";
import { ComplexLinkedList } from "./complex/linked_list";
import { ComplexMethod } from "./complex/method";
import { ComplexMutex } from "./complex/mutex";
import { ComplexPointer } from "./complex/pointer";
import { ComplexString } from "./complex/string";
import { ComplexWaitGroup } from "./complex/wait_group";
import { ContextControl } from "./context/control";
Expand Down Expand Up @@ -61,7 +60,6 @@ import { ControlMethodMember } from "./control/method_member";
import { ControlName } from "./control/name";
import { ControlNameAddress } from "./control/name_address";
import { ControlPopI } from "./control/pop_i";
import { ControlPostfix } from "./control/postfix";
import { ControlPushI } from "./control/push_i";
import { ControlRestoreEnvI } from "./control/restore_env_i";
import { ControlReturn } from "./control/return";
Expand Down Expand Up @@ -95,13 +93,11 @@ import {
TAG_PRIMITIVE_rune,
TAG_COMPLEX_string,
TAG_COMPLEX_linked_list,
TAG_COMPLEX_pointer,
TAG_CONTROL_name,
TAG_CONTROL_literal,
TAG_CONTROL_var,
TAG_CONTROL_assign,
TAG_CONTROL_unary,
TAG_CONTROL_postfix,
TAG_CONTROL_binary,
TAG_CONTROL_sequence,
TAG_CONTROL_call,
Expand Down Expand Up @@ -165,7 +161,6 @@ import {
TAG_CONTROL_chan_receive_stmt,
TAG_USER_type_struct_decl,
TAG_CONTROL_struct,
TAG_USER_type_method,
TAG_CONTROL_method,
TAG_CONTROL_member_address_i,
TAG_COMPLEX_method,
Expand Down Expand Up @@ -206,7 +201,6 @@ import { UserTypeChannel } from "./user/type/channel";
import { UserTypeFloat32 } from "./user/type/float32";
import { UserTypeFunction } from "./user/type/function";
import { UserTypeInt32 } from "./user/type/int32";
import { UserTypeMethod } from "./user/type/method";
import { UserTypeMutex } from "./user/type/mutex";
import { UserTypeNil } from "./user/type/nil";
import { UserTypeSlice } from "./user/type/slice";
Expand Down Expand Up @@ -234,8 +228,6 @@ function auto_cast(heap: Heap, address: number): HeapObject {
return new ComplexString(heap, address);
case TAG_COMPLEX_linked_list:
return new ComplexLinkedList(heap, address);
case TAG_COMPLEX_pointer:
return new ComplexPointer(heap, address);
case TAG_COMPLEX_array:
return new ComplexArray(heap, address);
case TAG_COMPLEX_function:
Expand All @@ -260,8 +252,6 @@ function auto_cast(heap: Heap, address: number): HeapObject {
return new ControlAssignI(heap, address);
case TAG_CONTROL_unary:
return new ControlUnary(heap, address);
case TAG_CONTROL_postfix:
return new ControlPostfix(heap, address);
case TAG_CONTROL_binary:
return new ControlBinary(heap, address);
case TAG_CONTROL_sequence:
Expand Down Expand Up @@ -428,8 +418,6 @@ function auto_cast(heap: Heap, address: number): HeapObject {
return new UserTypeBuiltin(heap, address);
case TAG_USER_type_struct_decl:
return new UserTypeStructDecl(heap, address);
case TAG_USER_type_method:
return new UserTypeMethod(heap, address);
case TAG_USER_type_mutex:
return new UserTypeMutex(heap, address);
case TAG_USER_type_wait_group:
Expand Down
Loading

0 comments on commit 9d60129

Please sign in to comment.