annotate runtime/progfoot.inc @ 377:93c28eee141e default tip

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 15 Aug 2015 22:45:33 -0700
parents 18598163e3ef
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
1
58
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
2 array * alloc_array(uint32_t num_els)
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
3 {
78
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
4 array * arr = GC_MALLOC(sizeof(array));
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
5 arr->header.meta = &array_meta;
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
6 arr->header.parent = NULL;
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
7 arr->storage = arr->size = num_els;
40
927fd7911a01 Add append message to array
Mike Pavone <pavone@retrodev.com>
parents: 38
diff changeset
8 if (num_els < 4) {
927fd7911a01 Add append message to array
Mike Pavone <pavone@retrodev.com>
parents: 38
diff changeset
9 arr->storage = 4;
927fd7911a01 Add append message to array
Mike Pavone <pavone@retrodev.com>
parents: 38
diff changeset
10 }
78
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
11 arr->data = GC_MALLOC(sizeof(object *) * arr->storage);
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
12 return arr;
58
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
13 }
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
14
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
15 object * make_array(uint32_t num_els, ...)
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
16 {
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
17 va_list els;
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
18 int i;
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
19 array * arr = alloc_array(num_els);
38
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
20 va_start(els, num_els);
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
21 for (i = 0; i < num_els; i++)
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
22 arr->data[i] = va_arg(els, object *);
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
23 va_end(els);
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
24 return &(arr->header);
e7be612fd3ae Very basic array support
Mike Pavone <pavone@retrodev.com>
parents: 35
diff changeset
25 }
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
27 object * make_list(uint32_t num_els, ...)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
28 {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
29 va_list els;
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
30 int i;
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
31 object * cur = mcall(METHOD_ID_EMPTY, 1, module_list);
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
32 va_start(els, num_els);
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
33 for (i = 0; i < num_els; i++)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
34 cur = mcall(METHOD_ID_CONS, 2, cur, va_arg(els, object *));
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
35 va_end(els);
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
36 return cur;
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
37 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents: 87
diff changeset
38
45
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
39 object * make_lambda(void * env, closure_func func)
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
40 {
78
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
41 lambda * ret = GC_MALLOC(sizeof(lambda));
45
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
42 ret->header.meta = &lambda_meta;
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
43 ret->header.parent = NULL;
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
44 ret->env = env;
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
45 ret->func = func;
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
46 return (object *) ret;
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
47 }
2a9c6eed0c70 Move closure/lambda object def into compiler rather than runtime code. Add while:do method to lambda
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
48
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 int main(int argc, char ** argv)
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 {
58
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
51 int i;
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
52 string * arg;
78
abc6f3d644a4 Use Boehm-GC for garbage collection. Also make no_impl print on stderr rather than standard in and return a non-zero error code.
Mike Pavone <pavone@retrodev.com>
parents: 58
diff changeset
53 GC_INIT();
58
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
54 array * arr = alloc_array(argc);
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
55 for (i = 0; i < argc; ++i) {
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
56 arg = (string *)make_object(&string_meta, NULL, 0);
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
57 arg->data = argv[i];
87
25bc8a5ab41e Improve llMessage a bit and move implementation of string into string.tp module using llMessage. Update TASKS list
Mike Pavone <pavone@retrodev.com>
parents: 78
diff changeset
58 arg->bytes = arg->len = strlen(argv[i]);
58
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
59 arr->data[i] = &(arg->header);
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
60 }
7b454d100dc8 Add length method to array. Pass array of arguments to main method. Initialize parent field of environment struct for closures
Mike Pavone <pavone@retrodev.com>
parents: 45
diff changeset
61 object * ret = mcall(METHOD_ID_MAIN, 2, mainModule(), &(arr->header));
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 if (ret->meta == &obj_int32_meta) {
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 obj_int32 * reti32 = (obj_int32 *) ret;
44
9dd370530f69 Fix escape codes in string literals. Don't print out the return value of main method. Fixup fib example to use print: method. Cleanup error handling in compiler slightly
Mike Pavone <pavone@retrodev.com>
parents: 40
diff changeset
64 return reti32->num;
31
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 return 0;
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 }
668f533e5284 Add initial version of C backend
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68