diff --git a/quark/test/emit/builtin_numbers.out b/quark/test/emit/builtin_numbers.out new file mode 100644 index 00000000..8376c68f --- /dev/null +++ b/quark/test/emit/builtin_numbers.out @@ -0,0 +1,4 @@ +3 +0 +3 +0 diff --git a/quark/test/emit/builtin_numbers.q b/quark/test/emit/builtin_numbers.q new file mode 100644 index 00000000..28eedac0 --- /dev/null +++ b/quark/test/emit/builtin_numbers.q @@ -0,0 +1,46 @@ +class TestInt { + void run() { + Map map = new Map(); + int b = 3; + print(b.toString()); + print(self.func().toString()); + } + int func() { + return new int(); + } +} + +class TestLong { + void run() { + Map map = new Map(); + // the following declaration does not translate to java + // xfail:java + long b = 3; + print(b.toString()); + print(self.func().toString()); + } + long func() { + return new long(); + } +} + +class Test { + void run() { + Map map = new Map(); +// this bit crashes the compiler so we need an xfail for now +// NOTE: the above xfail:java masks this xfail +// T b = 3; +// print(b.toString()); +// print(self.func().toString()); + } +// T func() { +// return new T(); +// } +} + +void main() { + new TestInt().run(); + new TestLong().run(); + new Test().run(); + new Test().run(); +} diff --git a/quark/test/emit/builtin_numbers/js/builtin_numbers.js b/quark/test/emit/builtin_numbers/js/builtin_numbers.js new file mode 100644 index 00000000..0983cfb2 --- /dev/null +++ b/quark/test/emit/builtin_numbers/js/builtin_numbers.js @@ -0,0 +1,69 @@ +var _qrt = require("quark_runtime.js"); + +// CLASS TestInt +function TestInt() { + this.__init_fields__(); +} +exports.TestInt = TestInt; + +function TestInt__init_fields__() {} +TestInt.prototype.__init_fields__ = TestInt__init_fields__; + +function TestInt_run() { + var map = new Map(); + var b = 3; + _qrt.print(_qrt.toString(b)); + _qrt.print(_qrt.toString((this).func())); +} +TestInt.prototype.run = TestInt_run; + +function TestInt_func() { + return new Number(); +} +TestInt.prototype.func = TestInt_func; + +// CLASS TestLong +function TestLong() { + this.__init_fields__(); +} +exports.TestLong = TestLong; + +function TestLong__init_fields__() {} +TestLong.prototype.__init_fields__ = TestLong__init_fields__; + +function TestLong_run() { + var map = new Map(); + var b = 3; + _qrt.print(_qrt.toString(b)); + _qrt.print(_qrt.toString((this).func())); +} +TestLong.prototype.run = TestLong_run; + +function TestLong_func() { + return new Number(); +} +TestLong.prototype.func = TestLong_func; + +// CLASS Test +function Test() { + this.__init_fields__(); +} +exports.Test = Test; + +function Test__init_fields__() {} +Test.prototype.__init_fields__ = Test__init_fields__; + +function Test_run() { + var map = new Map(); +} +Test.prototype.run = Test_run; + +function main() { + (new TestInt()).run(); + (new TestLong()).run(); + (new Test()).run(); + (new Test()).run(); +} +exports.main = main; + +main(); diff --git a/quark/test/emit/builtin_numbers/js/node_modules/quark_runtime.js b/quark/test/emit/builtin_numbers/js/node_modules/quark_runtime.js new file mode 100644 index 00000000..7fe07596 --- /dev/null +++ b/quark/test/emit/builtin_numbers/js/node_modules/quark_runtime.js @@ -0,0 +1,203 @@ +// Quark Runtime +/* jshint node: true */ + +(function () { + "use strict"; + + exports.util = require("util"); + + function quark_toString(value) { + if (value === null) { + return "null"; + } + if (Array.isArray(value)) { + return "[" + value.map(quark_toString).join(", ") + "]"; + } + return value.toString(); + } + exports.toString = quark_toString; + + function print(message) { + console.log(quark_toString(message)); + } + exports.print = print; + + function modulo(a, b) { + return (a % b + b) % b; + } + exports.modulo = modulo; + + function map_get(m, key) { + if (m.has(key)) { + return m.get(key); + } + return null; + } + exports.map_get = map_get; + + var execSync = require("child_process").execSync; + + function url_get(url) { + var resBuffer = execSync('curl -s -w "\n\n%{http_code}" ' + url); + var res = resBuffer.toString("UTF-8"); + if (res.substr(-5) === "\n\n200") { + return res.substr(0, res.length - 5); + } + return "error"; + } + exports.url_get = url_get; + + function sleep(seconds) { + execSync("sleep " + seconds); + } + exports.sleep = sleep; + + function JSONObject() { + this.value = null; + } + exports.JSONObject = JSONObject; + + function JSONObject_getType() { + var t = typeof this.value; + if (t === "object") { + if (this.value === null) + return "null"; + if (Array.isArray(this.value)) + return "list"; + } + return t; + } + JSONObject.prototype.getType = JSONObject_getType; + + function JSONObject_wrap(value) { + var j = new JSONObject(); + j.value = value; + return j; + } + + function JSONObject_getObjectItem(key) { + var value = this.value[key]; + if (value === undefined) { + return this.undefined(); + } + return JSONObject_wrap(value); + } + + function JSONObject_getListItem(index) { + var value = this.value[index]; + if (value === undefined) { + return this.undefined(); + } + return JSONObject_wrap(value); + } + JSONObject.prototype.getListItem = JSONObject_getListItem; + + function JSONObject_getString() { + if (typeof this.value !== 'string') { + return this.undefined(); + } + return this.value; + } + JSONObject.prototype.getString = JSONObject_getString; + + function JSONObject_getNumber() { + if (typeof this.value !== 'number') { + return this.undefined(); + } + return this.value; + } + JSONObject.prototype.getNumber = JSONObject_getNumber; + + function JSONObject_getBool() { + if (typeof this.value !== 'boolean') { + return this.undefined(); + } + return this.value ? 1 : 0; + } + JSONObject.prototype.getBool = JSONObject_getBool; + + function JSONObject_isNull() { + return this.value === null; + } + JSONObject.prototype.isNull = JSONObject_isNull; + + function JSONObject_undefined() { + var u = new JSONObject(); + u.value = undefined; + return u; + } + JSONObject.prototype.undefined = JSONObject_undefined; + + function JSONObject_toString() { + return JSON.stringify(this.value); + } + JSONObject.prototype.toString = JSONObject_toString; + + + function JSONObject_setString(value) { + this.value = value; + return this; + } + JSONObject.prototype.setString = JSONObject_setString; + + function JSONObject_setNumber(value) { + this.value = value; + return this; + } + JSONObject.prototype.setNumber = JSONObject_setNumber; + + function JSONObject_setBool(value) { + this.value = !!value; + return this; + } + JSONObject.prototype.setBool = JSONObject_setBool; + + function JSONObject_setNull() { + this.value = null; + return this; + } + JSONObject.prototype.setNull = JSONObject_setNull; + + function JSONObject_setObject() { + this.value = {}; + return this; + } + JSONObject.prototype.setObject = JSONObject_setObject; + + function JSONObject_setList() { + this.value = []; + return this; + } + JSONObject.prototype.setList = JSONObject_setList; + + function JSONObject_setObjectItem(key, value) { + if (this.getType() !== 'object') { + this.value = {}; + } + this.value[key] = value.value; + return this; + } + JSONObject.prototype.setObjectItem = JSONObject_setObjectItem; + + function JSONObject_setListItem(index, value) { + if (this.getType() !== 'list') { + this.value = []; + } + for(var i = this.value.length; i < index - 1; i++) { + this.value[i] = null; + } + this.value[index] = value.value; + return this; + } + JSONObject.prototype.setListItem = JSONObject_setListItem; + + function json_from_string(json) { + var raw = JSON.parse(json); + var value = new JSONObject(); + value.value = raw; + return value; + } + + exports.json_from_string = json_from_string; + +})(); diff --git a/quark/test/emit/builtin_numbers/py/builtin_numbers.py b/quark/test/emit/builtin_numbers/py/builtin_numbers.py new file mode 100644 index 00000000..f79b8ea5 --- /dev/null +++ b/quark/test/emit/builtin_numbers/py/builtin_numbers.py @@ -0,0 +1,48 @@ +from quark_runtime import * + +class TestInt(object): + def _init(self): pass + def __init__(self): self._init() + + def run(self): + map = _Map(); + b = 3; + _println(str(b)); + _println(str((self).func())); + + def func(self): + return int() + + +class TestLong(object): + def _init(self): pass + def __init__(self): self._init() + + def run(self): + map = _Map(); + b = 3; + _println(str(b)); + _println(str((self).func())); + + def func(self): + return long() + + +class Test(object): + def _init(self): pass + def __init__(self): self._init() + + def run(self): + map = _Map(); + + + +def main(): + (TestInt()).run(); + (TestLong()).run(); + (Test()).run(); + (Test()).run(); + + +if __name__ == "__main__": + main() diff --git a/quark/test/emit/builtin_numbers/py/quark_runtime.py b/quark/test/emit/builtin_numbers/py/quark_runtime.py new file mode 100644 index 00000000..81e2e7a4 --- /dev/null +++ b/quark/test/emit/builtin_numbers/py/quark_runtime.py @@ -0,0 +1,154 @@ +# Quark Runtime +# Usage: from quark_runtime import * +# This brings in the stuff mentioned in __all__ below. +# The wrong way to do this, but minimizes the code change. + +import os # unused? +import sys +import time # used by the builtin now() macro +import urllib2 +import json +import collections + +__all__ = "os sys time _Map _List _println _url_get _JSONObject".split() + + +_Map = dict + + +class _List(list): + def __repr__(self): + return "[%s]" % ", ".join([str(e) for e in self]) + + +def _println(obj): + if obj is None: + sys.stdout.write("null\n") + else: + sys.stdout.write("%s\n" % obj) + + +def _url_get(url): + try: + return urllib2.urlopen(url).read() + except Exception: + return "error" + +class _JSONObject(object): + _backend = json + _dict = collections.OrderedDict + _undefined = object() + + def __init__(self): + self.value = None + + @classmethod + def _wrap(cls, value): + wrapped = cls() + wrapped.value = value + return wrapped + + def getType(self): + if isinstance(self.value, dict): + return 'object' + elif isinstance(self.value, (list, tuple)): + return 'list' + elif isinstance(self.value, (str, unicode)): + return 'string' + elif isinstance(self.value, (int,float,long)): + return 'number' + elif isinstance(self.value, bool): + return 'bool' + elif self.value is None: + return 'null' + else: + raise TypeError("Unknown JSONObject type " + str(type(self.value))) + + def getObjectItem(self, key): + try: + if isinstance(self.value, dict): + return self._wrap(self.value[key]) + except (KeyError, TypeError): + pass + return self.undefined() + + def getListItem(self, index): + try: + if isinstance(self.value, (list, tuple)): + return self._wrap(self.value[index]) + except (KeyError, IndexError, TypeError): + pass + return self.undefined() + + def getString(self): + if isinstance(self.value, (str, unicode)): + return self.value + else: + return self.undefined() + + def getNumber(self): + if isinstance(self.value, (int, long, float)): + return self.value + else: + return self.undefined() + + def getBool(self): + if isinstance(self.value, bool): + return self.value + else: + return self.undefined() + + def isNull(self): + return self.value is None + + def undefined(self): + return self._wrap(self._undefined) + + def toString(self): + return self._backend.dumps(self.value, separators=(',', ':')) + + def setString(self, value): + self.value = value + return self + + def setNumber(self, value): + self.value = value + return self + + def setBool(self, value): + self.value = bool(value) + return self + + def setNull(self): + self.value = None + return self + + def setObject(self): + self.value = self._dict() + return self + + def setList(self): + self.value = [] + return self + + def setObjectItem(self, key, value): + if not isinstance(self.value, dict): + self.value = self._dict() + self.value[key] = value.value + return self + + def setListItem(self, index, value): + if not isinstance(self.value, list): + if isinstance(self.value, tuple): + self.value = list(self.value) + else: + self.value = [] + missing = index - len(self.value) + 1 + if missing > 0: + self.value.extend([None] * missing) + self.value[index] = value.value + return self + + @classmethod + def parse(cls, value): + return cls._wrap(cls._backend.loads(value, object_pairs_hook=cls._dict))