Skip to content

Commit

Permalink
fix stdlib & other
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhao28 committed Jun 29, 2015
1 parent 64f785e commit f33ef5d
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 74 deletions.
2 changes: 1 addition & 1 deletion demo/debug.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if process.argv.length > 2
else
tests = JSON.parse(fs.readFileSync("test/test.json"))

cppFile = tests.tests[testName].cpp[0]
cppFile = tests.tests[testName].cases[0].cpp
input = tests.tests[testName].cases[0].in or ""

code = fs.readFileSync("./test/" + cppFile)
Expand Down
2 changes: 1 addition & 1 deletion demo/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if (process.argv.length > 2) {
input = "";
} else {
tests = JSON.parse(fs.readFileSync("test/test.json"));
cppFile = tests.tests[testName].cpp[0];
cppFile = tests.tests[testName].cases[0].cpp;
input = tests.tests[testName].cases[0]["in"] || "";
code = fs.readFileSync("./test/" + cppFile);
}
Expand Down
54 changes: 43 additions & 11 deletions dist/JSCPP.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ast.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions lib/includes/cstdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = {
str = rt.getStringFromCharArray(str);
try {
ret = eval(str);
if (ret !== void 0) {
if (ret != null) {
console.log(ret);
}
return rt.val(rt.intTypeLiteral, 1);
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = {
return rt.raiseException("base must be an array");
}
};
cmpType = this.functionPointerType(rt.intTypeLiteral, [rt.voidPointerType, rt.voidPointerType]);
cmpType = rt.functionPointerType(rt.intTypeLiteral, [rt.voidPointerType, rt.voidPointerType]);
rt.regFunc(_bsearch, "global", "bsearch", [rt.voidPointerType, rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType], rt.voidPointerType);
_qsort = function(rt, _this, base, num, size, cmp) {
var L, ele, i, j, len, wrapper;
Expand All @@ -127,13 +127,27 @@ module.exports = {
return rt.raiseException("base must be an array");
}
};
rt.regFunc(_qsort, "global", "qsort", [rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType]);
rt.regFunc(_qsort, "global", "qsort", [rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType], rt.voidTypeLiteral);
_abs = function(rt, _this, n) {
return rt.val(rt.intTypeLiteral, Math.abs(n.v));
};
rt.regFunc(_abs, "global", "abs", [rt.intTypeLiteral], rt.intTypeLiteral);
_div = function(rt, _this, numer, denom) {
return rt.val(rt.intTypeLiteral, Math.floor(numer / denom));
var quot, rem;
if (denom.v === 0) {
rt.raiseException("divided by zero");
}
quot = rt.val(rt.intTypeLiteral, Math.floor(numer.v / denom.v));
rem = rt.val(rt.intTypeLiteral, numer.v % denom.v);
return {
t: div_t_t,
v: {
members: {
quot: quot,
rem: rem
}
}
};
};
div_t_t = rt.newClass("div_t", [
{
Expand All @@ -150,7 +164,21 @@ module.exports = {
};
rt.regFunc(_labs, "global", "labs", [rt.longTypeLiteral], rt.longTypeLiteral);
_ldiv = function(rt, _this, numer, denom) {
return rt.val(rt.longTypeLiteral, Math.floor(numer / denom));
var quot, rem;
if (denom.v === 0) {
rt.raiseException("divided by zero");
}
quot = rt.val(rt.longTypeLiteral, Math.floor(numer.v / denom.v));
rem = rt.val(rt.longTypeLiteral, numer.v % denom.v);
return {
t: ldiv_t_t,
v: {
members: {
quot: quot,
rem: rem
}
}
};
};
ldiv_t_t = rt.newClass("ldiv_t", [
{
Expand Down
6 changes: 4 additions & 2 deletions lib/includes/iostream.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ module.exports = {
}
len = r[0].length;
_cin.v.failbit = len === 0;
t.v = rt.val(t.t, v).v;
_cin.v.buf = b.substring(len);
if (!_cin.v.failbit) {
t.v = rt.val(t.t, v).v;
_cin.v.buf = b.substring(len);
}
return _cin;
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/rt.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,11 @@ CRuntime.prototype.getTypeSigniture = function(type) {
} else if (type.ptrType === "array") {
ret += "!" + this.getTypeSigniture(type.eleType);
} else if (type.ptrType === "function") {
ret += "#" + this.getTypeSigniture(type.retType) + "!" + type.signature.map(function(e) {
return this.getTypeSigniture(e);
}).join(",");
ret += "#" + this.getTypeSigniture(type.retType) + "!" + type.signature.map((function(_this) {
return function(e) {
return _this.getTypeSigniture(e);
};
})(this)).join(",");
}
ret += "}";
}
Expand Down
2 changes: 1 addition & 1 deletion pegjs/ast.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ ConditionalExpression
= a:LogicalORExpression b:(QUERY Expression COLON LogicalORExpression)* {
var ret = a;
for (var i=0;i<b.length;i++) {
ret = addPositionInfo({type:'ConditionalExpression', cond:ret, t:b[1], f:b[3]});
ret = addPositionInfo({type:'ConditionalExpression', cond:ret, t:b[i][1], f:b[i][3]});
}
return ret;
}
Expand Down
26 changes: 21 additions & 5 deletions src/includes/cstdlib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports =
str = rt.getStringFromCharArray str
try
ret = eval str
if ret isnt undefined
if ret?
console.log ret
rt.val(rt.intTypeLiteral, 1)
catch e
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports =
else
rt.raiseException "base must be an array"

cmpType = this.functionPointerType(rt.intTypeLiteral, [rt.voidPointerType, rt.voidPointerType])
cmpType = rt.functionPointerType(rt.intTypeLiteral, [rt.voidPointerType, rt.voidPointerType])
rt.regFunc(_bsearch, "global", "bsearch", [rt.voidPointerType, rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType], rt.voidPointerType)

_qsort = (rt, _this, base, num, size, cmp) ->
Expand All @@ -110,14 +110,22 @@ module.exports =
else
rt.raiseException "base must be an array"

rt.regFunc(_qsort, "global", "qsort", [rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType])
rt.regFunc(_qsort, "global", "qsort", [rt.voidPointerType, rt.intTypeLiteral, rt.intTypeLiteral, cmpType], rt.voidTypeLiteral)

_abs = (rt, _this, n) -> rt.val(rt.intTypeLiteral, Math.abs(n.v))

rt.regFunc(_abs, "global", "abs", [rt.intTypeLiteral], rt.intTypeLiteral)

_div = (rt, _this, numer, denom) ->
rt.val(rt.intTypeLiteral, Math.floor(numer / denom))
if denom.v is 0
rt.raiseException "divided by zero"
quot = rt.val(rt.intTypeLiteral, Math.floor(numer.v / denom.v))
rem = rt.val(rt.intTypeLiteral, numer.v % denom.v)
t: div_t_t
v:
members:
quot: quot
rem: rem

div_t_t = rt.newClass("div_t", [
{
Expand All @@ -136,7 +144,15 @@ module.exports =
rt.regFunc(_labs, "global", "labs", [rt.longTypeLiteral], rt.longTypeLiteral)

_ldiv = (rt, _this, numer, denom) ->
rt.val(rt.longTypeLiteral, Math.floor(numer / denom))
if denom.v is 0
rt.raiseException "divided by zero"
quot = rt.val(rt.longTypeLiteral, Math.floor(numer.v / denom.v))
rem = rt.val(rt.longTypeLiteral, numer.v % denom.v)
t: ldiv_t_t
v:
members:
quot: quot
rem: rem

ldiv_t_t = rt.newClass("ldiv_t", [
{
Expand Down
5 changes: 3 additions & 2 deletions src/includes/iostream.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ module.exports = load: (rt) ->
rt.raiseException ">> operator in istream cannot accept " + rt.makeTypeString(t.t)
len = r[0].length
_cin.v.failbit = len is 0
t.v = rt.val(t.t, v).v
_cin.v.buf = b.substring(len)
if not _cin.v.failbit
t.v = rt.val(t.t, v).v
_cin.v.buf = b.substring(len)
return _cin

_cinString = (rt, _cin, t) ->
Expand Down
2 changes: 1 addition & 1 deletion src/rt.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ CRuntime::getTypeSigniture = (type) ->
else if type.ptrType is "array"
ret += "!" + @getTypeSigniture(type.eleType)
else if type.ptrType is "function"
ret += "#" + @getTypeSigniture(type.retType) + "!" + type.signature.map((e) ->
ret += "#" + @getTypeSigniture(type.retType) + "!" + type.signature.map((e) =>
@getTypeSigniture e
).join(",")
ret += "}"
Expand Down
15 changes: 15 additions & 0 deletions test/stdlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include "stdlib.h"
using namespace std;
int main() {
cout << atof("1.234") << endl;
cout << atoi("1234") << endl;
cout << atol("1234") << endl;
cout << abs(-1234) << endl;
cout << div(7, 3).quot << endl;
cout << div(7, 3).rem << endl;
cout << labs(-1234) << endl;
cout << ldiv(7, 3).quot << endl;
cout << ldiv(7, 3).rem << endl;
return 0;
}
29 changes: 29 additions & 0 deletions test/string2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int n;
cin >> n;
char word[50];
int charsThisLine = 0;
for (int i = 0; i < n; i++) {
cin >> word;
int len = strlen(word);
bool prefixSpace = charsThisLine > 0;
int expectedLen = charsThisLine + (prefixSpace ? 1 : 0) + len;
if (expectedLen <= 80) {
if (prefixSpace) {
charsThisLine += 1;
cout << ' ';
}
cout << word;
charsThisLine += len;
} else {
cout << endl << word;
charsThisLine = len;
}
}
cout << endl;
return 0;
}
12 changes: 5 additions & 7 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ skippedTest = []

doTest = (test, cb) ->
success = true
cppFiles = test.cpp
cases = test.cases
for cppFile in cppFiles
code = fs.readFileSync testFolder + cppFile
_describe "source #{cppFile}", ->
doSource code, cases, (result) ->
success = success and result
doCases cases, (result) ->
success = success and result
cb success

doSource = (code, cases, cb) ->
doCases = (cases, cb) ->
success = true
for sample, i in cases
cppFile = sample.cpp
code = fs.readFileSync testFolder + cppFile
input = sample.in or ""
expected = prepareOutput(sample.out) or ""
except = prepareOutput(sample.exception)
Expand Down
Loading

0 comments on commit f33ef5d

Please sign in to comment.