git @ Cat's Eye Technologies Castile / 0f57707
Make tests independent of tagged value representation. catseye 12 years ago
4 changed file(s) with 39 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
924924 | fun main() {
925925 | var a = 20;
926926 | var b = 30;
927 | a + b as integer in union(integer, string)
928 | }
929 = ('Type(integer:)', 50)
930
931 All good looks like this:
932
933 | fun main() {
934 | var a = 20;
935 | var b = 30;
936 | a + b as integer in union(integer, string)
937 | }
938 = ('Type(integer:)', 50)
939
940 Union types.
927 | var c = a + b as integer in union(integer, string)
928 | print("ok")
929 | }
930 = ok
931
932 Values of union type can be passed to functions.
941933
942934 | fun foo(a, b: union(integer, string)) {
943935 | a + 1
970962 | }
971963 = 337
972964
965 The expression in a `typecase` must be a variable.
966
967 | main = fun() {
968 | var a = 333 as integer in union(integer, string);
969 | typecase 333 is integer {
970 | print("what?")
971 | };
972 | }
973 ? identifier
974
975 The expression in a `typecase` can be an argument.
976
977 | fun wat(j: union(integer, string)) {
978 | typecase j is integer {
979 | print("integer")
980 | };
981 | }
982 | main = fun() {
983 | wat(444 as integer in union(integer, string))
984 | }
985 = integer
986
987 The expression in a `typecase` cannot effectively be a global, as globals
988 must be literals and there is no way (right now) to make a literal of union
989 type.
990
973991 This is a very strange case in the language. Thankfully, assignment
974992 typechecks as void, without any automatic promotion to the union type...
975993
9891007
9901008 ### Struct Types + Union Types ###
9911009
992 Union types may be used to make fields "nullable", so that
993 you can actually make finite, recursive data types.
1010 Union types may be used to make fields of a struct "nullable", so that
1011 you can in actuality create recursive, but finite, data structures.
9941012
9951013 | struct list {
9961014 | value: string;
1212 Builtins: `int`, `str`, `chr`, `ord`.
1313
1414 Tests for empty structs. Demo of "typed enum" (union of empty structs.)
15
16 Type promotion with higher precedence? So that it can be used at toplevel.
1517
1618 ### Implementation ###
1719
2222
2323 /*
2424 var stdin = process.openStdin();
25 // node.js does not make this easy. forgetting about it for now.
25 // node.js does not make this easy -- not unless I want to
26 // generate code in cps! forgetting about it for now.
2627 var input = function(s) {
2728 var answer = undefined;
2829 stdin.on('data', function(chunk) { answer = chunk; });
4647 return "";
4748 } else if (o === null) {
4849 return "None";
49 } else if (typeof o === "object") {
50 var s = "(";
51 for (var i = 0; i < o.length; i++) {
52 s += repr(o[i]);
53 if (i != o.length - 1) { s += ', '; }
54 }
55 s += ")";
56 return s;
5750 } else {
5851 return o;
5952 }
5757 return "None"
5858 elsif o.is_a? String
5959 return "'" + o + "'"
60 elsif o.is_a? Array
61 if o.length == 0 then return "()" end
62 h = "("
63 for c in o[0..o.length-2] do
64 h += repr(c) + ", "
65 end
66 return h + repr(o[o.length-1]) + ")"
6760 else
6861 return o.to_s
6962 end
168161 elif ast.type == 'Make':
169162 self.out.write('{')
170163 self.commas(ast.children[1:])
171 self.out.write(", '_fieldnames', [")
172 for fieldinit in ast.children[1:-1]:
173 self.out.write("'%s', " % fieldinit.value)
174 self.out.write("'%s'" % ast.children[-1].value)
175 self.out.write(']}')
164 self.out.write('}')
176165 elif ast.type == 'FieldInit':
177166 self.out.write("'%s'," % ast.value)
178167 self.compile(ast.children[0])