diff --git a/README.markdown b/README.markdown index 6813267..293eb42 100644 --- a/README.markdown +++ b/README.markdown @@ -925,20 +925,12 @@ | fun main() { | var a = 20; | var b = 30; - | a + b as integer in union(integer, string) - | } - = ('Type(integer:)', 50) - -All good looks like this: - - | fun main() { - | var a = 20; - | var b = 30; - | a + b as integer in union(integer, string) - | } - = ('Type(integer:)', 50) - -Union types. + | var c = a + b as integer in union(integer, string) + | print("ok") + | } + = ok + +Values of union type can be passed to functions. | fun foo(a, b: union(integer, string)) { | a + 1 @@ -971,6 +963,32 @@ | } = 337 +The expression in a `typecase` must be a variable. + + | main = fun() { + | var a = 333 as integer in union(integer, string); + | typecase 333 is integer { + | print("what?") + | }; + | } + ? identifier + +The expression in a `typecase` can be an argument. + + | fun wat(j: union(integer, string)) { + | typecase j is integer { + | print("integer") + | }; + | } + | main = fun() { + | wat(444 as integer in union(integer, string)) + | } + = integer + +The expression in a `typecase` cannot effectively be a global, as globals +must be literals and there is no way (right now) to make a literal of union +type. + This is a very strange case in the language. Thankfully, assignment typechecks as void, without any automatic promotion to the union type... @@ -990,8 +1008,8 @@ ### Struct Types + Union Types ### -Union types may be used to make fields "nullable", so that -you can actually make finite, recursive data types. +Union types may be used to make fields of a struct "nullable", so that +you can in actuality create recursive, but finite, data structures. | struct list { | value: string; diff --git a/TODO.markdown b/TODO.markdown index 5c49a41..c90919a 100644 --- a/TODO.markdown +++ b/TODO.markdown @@ -13,6 +13,8 @@ Builtins: `int`, `str`, `chr`, `ord`. Tests for empty structs. Demo of "typed enum" (union of empty structs.) + +Type promotion with higher precedence? So that it can be used at toplevel. ### Implementation ### diff --git a/src/castile/backends/javascript.py b/src/castile/backends/javascript.py index 79ac321..bfa2694 100644 --- a/src/castile/backends/javascript.py +++ b/src/castile/backends/javascript.py @@ -23,7 +23,8 @@ /* var stdin = process.openStdin(); -// node.js does not make this easy. forgetting about it for now. +// node.js does not make this easy -- not unless I want to +// generate code in cps! forgetting about it for now. var input = function(s) { var answer = undefined; stdin.on('data', function(chunk) { answer = chunk; }); @@ -47,14 +48,6 @@ return ""; } else if (o === null) { return "None"; - } else if (typeof o === "object") { - var s = "("; - for (var i = 0; i < o.length; i++) { - s += repr(o[i]); - if (i != o.length - 1) { s += ', '; } - } - s += ")"; - return s; } else { return o; } diff --git a/src/castile/backends/ruby.py b/src/castile/backends/ruby.py index 6ef9616..c53b4af 100644 --- a/src/castile/backends/ruby.py +++ b/src/castile/backends/ruby.py @@ -58,13 +58,6 @@ return "None" elsif o.is_a? String return "'" + o + "'" - elsif o.is_a? Array - if o.length == 0 then return "()" end - h = "(" - for c in o[0..o.length-2] do - h += repr(c) + ", " - end - return h + repr(o[o.length-1]) + ")" else return o.to_s end @@ -169,11 +162,7 @@ elif ast.type == 'Make': self.out.write('{') self.commas(ast.children[1:]) - self.out.write(", '_fieldnames', [") - for fieldinit in ast.children[1:-1]: - self.out.write("'%s', " % fieldinit.value) - self.out.write("'%s'" % ast.children[-1].value) - self.out.write(']}') + self.out.write('}') elif ast.type == 'FieldInit': self.out.write("'%s'," % ast.value) self.compile(ast.children[0])