introjs
Advanced tools
Comparing version 0.1.4 to 0.2.0
'use strict'; | ||
module.exports = { | ||
new_ints$: { | ||
type: 'Function', | ||
builtin: true, | ||
paramTypes: [ | ||
'Int' | ||
], | ||
resultType: 'Int[]' | ||
}, | ||
read$: { | ||
@@ -13,0 +5,0 @@ type: 'Function', |
@@ -144,2 +144,6 @@ 'use strict'; | ||
Context.prototype.pos = function (value) { | ||
return value; | ||
}; | ||
Context.prototype.bnot = function (value) { | ||
@@ -234,7 +238,12 @@ return ~value; | ||
Context.prototype.new_ints$ = function (length) { | ||
var values = new Array(length); | ||
for (var i = 0; i < length; i++) { | ||
values[i] = 0; | ||
Context.prototype.zeros = function () { | ||
var n = arguments[0]; | ||
if (arguments.length === 1) { | ||
return new Array(n).fill(0); | ||
} | ||
var values = []; | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
for (var i = 0; i < n; i++) { | ||
values.push(this.zeros.apply(this, args)); | ||
} | ||
return values; | ||
@@ -241,0 +250,0 @@ }; |
@@ -29,2 +29,3 @@ 'use strict'; | ||
'-': 'neg', | ||
'+': 'pos', | ||
'~': 'bnot', | ||
@@ -236,2 +237,3 @@ '!': 'not', | ||
} | ||
counter.dataType = 'Int'; | ||
iterateCall = contextCall('range', args, { | ||
@@ -241,3 +243,3 @@ start: stmt.right.loc.start, | ||
}); | ||
} else if (first.dataType == 'Int[]') { | ||
} else if (isArrayType(first.dataType)) { | ||
if (stmt.last) { | ||
@@ -247,2 +249,3 @@ throw newConvertError('FOR_LAST_FOUND', stmt.last.loc); | ||
iterateCall = contextCall('iter', [first], first.loc); | ||
counter.dataType = toElementType(first.dataType); | ||
} else { | ||
@@ -252,3 +255,8 @@ throw newConvertError('FOR_FIRST_VOID', first.loc); | ||
loopLevel++; | ||
var body = convertBlockStatement(stmt.body); | ||
var symbolTable = {}; | ||
symbolTable[counter.name] = { | ||
type: 'Variable', | ||
dataType: counter.dataType, | ||
} | ||
var body = convertBlockStatement(stmt.body, symbolTable); | ||
loopLevel--; | ||
@@ -263,5 +271,10 @@ return { | ||
type: 'VariableDeclarator', | ||
id: counter, | ||
init: null, | ||
}, | ||
{ | ||
type: 'VariableDeclarator', | ||
id: iterator, | ||
init: iterateCall, | ||
} | ||
}, | ||
], | ||
@@ -370,2 +383,4 @@ kind: 'var' | ||
return convertIdentifier(expr); | ||
case 'NewExpression': | ||
return convertNewExpression(expr); | ||
case 'ArrayExpression': | ||
@@ -442,3 +457,3 @@ return convertArrayExpression(expr); | ||
if (expr.operator === '$') { | ||
if (arg.dataType !== 'Int[]') { | ||
if (!isArrayType(arg.dataType)) { | ||
throw newConvertError('UNARY_ARGUMENT_NOT_ARRAY', arg.loc); | ||
@@ -490,3 +505,3 @@ } | ||
var object = convertExpression(expr.object); | ||
if (object.dataType !== 'Int[]') { | ||
if (!isArrayType(object.dataType)) { | ||
throw newConvertError('INDEXED_MEMBER_OBJECT_NOT_ARRAY', object.loc); | ||
@@ -499,3 +514,3 @@ } | ||
var call = contextCall('getAt', [object, property], expr.loc); | ||
call.dataType = 'Int'; | ||
call.dataType = toElementType(object.dataType); | ||
return call; | ||
@@ -530,8 +545,39 @@ } | ||
function convertNewExpression(expr) { | ||
var args = []; | ||
var dataType = expr.callee; | ||
for (var i in expr.arguments) { | ||
var arg = convertExpression(expr.arguments[i]); | ||
if (arg.dataType !== 'Int') { | ||
throw newConvertError('NEW_INDEX_BAD_TYPE', arg.loc); | ||
} | ||
args.push(arg); | ||
dataType = toArrayType(dataType); | ||
} | ||
var call = contextCall('zeros', args, expr.loc); | ||
call.dataType = dataType; | ||
return call; | ||
} | ||
function convertArrayExpression(expr) { | ||
var elems = []; | ||
for (var i in expr.elements) { | ||
if (expr.elements.length === 0) { | ||
return { | ||
type: expr.type, | ||
loc: expr.loc, | ||
elements: elems, | ||
dataType: 'Int[]', | ||
}; | ||
} | ||
var elem = convertExpression(expr.elements[0]); | ||
var dataType = elem.dataType | ||
if (!dataType) { | ||
throw newConvertError('ARRAY_ELEMENT_VOID', elem.loc); | ||
} | ||
elems.push(elem); | ||
for (var i = 1; i < expr.elements.length; i++) { | ||
var elem = convertExpression(expr.elements[i]); | ||
if (elem.dataType !== 'Int') { | ||
throw newConvertError('ARRAY_ELEMENT_BAD_TYPE', elem.loc); | ||
if (elem.dataType !== dataType) { | ||
throw newConvertError('ARRAY_ELEMENT_DIFFERENT_TYPE', elem.loc); | ||
} | ||
@@ -544,3 +590,3 @@ elems.push(elem); | ||
elements: elems, | ||
dataType: 'Int[]', | ||
dataType: toArrayType(dataType), | ||
}; | ||
@@ -570,34 +616,67 @@ } | ||
var operator = expr.operator; | ||
var operatorName = binaryOperatorNames[expr.operator.slice(0, -1)]; | ||
var index = expr.index; | ||
if (index) { | ||
if (left.dataType !== 'Int[]') { | ||
throw newConvertError('INDEXED_ASSIGNMENT_LEFT_NOT_ARRAY', left.loc); | ||
var operatorName = binaryOperatorNames[operator.slice(0, -1)]; | ||
if (expr.indices.length === 0) { | ||
if (right.dataType !== left.dataType) { | ||
throw newConvertError('ASSIGNMENT_RIGHT_BAD_TYPE', right.loc); | ||
} | ||
if (operatorName) { | ||
right = contextCall(operatorName, [left, right], expr.loc); | ||
operator = '='; | ||
} | ||
return { | ||
type: expr.type, | ||
loc: expr.loc, | ||
operator: operator, | ||
left: left, | ||
right: right, | ||
}; | ||
} | ||
if (operatorName) { | ||
if (right.dataType !== 'Int') { | ||
throw newConvertError('INDEXED_ASSIGNMENT_RIGHT_BAD_TYPE', right.loc); | ||
} | ||
index = convertExpression(index); | ||
operatorName += 'At'; | ||
} else { | ||
operatorName = 'setAt'; | ||
} | ||
var index; | ||
var i = 0; | ||
while (true) { | ||
index = convertExpression(expr.indices[i]); | ||
if (index.dataType !== 'Int') { | ||
throw newConvertError('INDEXED_ASSIGNMENT_INDEX_BAD_TYPE', index.loc); | ||
} | ||
return contextCall(operatorName ? operatorName + 'At' : 'setAt', [left, index, right], expr.loc); | ||
var dataType = left.dataType; | ||
if (!isArrayType(dataType)) { | ||
throw newConvertError('INDEXED_ASSIGNMENT_LEFT_NOT_ARRAY', left.loc); | ||
} | ||
if (i === expr.indices.length - 1) { | ||
break; | ||
} | ||
left = contextCall('getAt', [left, index], { | ||
start: left.loc.start, | ||
end: index.loc.end, | ||
}); | ||
left.dataType = toElementType(dataType); | ||
i++; | ||
} | ||
if (right.dataType !== left.dataType) { | ||
throw newConvertError('ASSIGNMENT_RIGHT_BAD_TYPE', right.loc); | ||
if (right.dataType !== toElementType(left.dataType)) { | ||
throw newConvertError('INDEXED_ASSIGNMENT_RIGHT_DIFFERENT_TYPE', right.loc); | ||
} | ||
if (operatorName) { | ||
right = contextCall(operatorName, [left, right], expr.loc); | ||
operator = '='; | ||
} | ||
return { | ||
type: expr.type, | ||
loc: expr.loc, | ||
operator: operator, | ||
left: left, | ||
right: right, | ||
}; | ||
return contextCall(operatorName, [left, index, right], expr.loc); | ||
} | ||
function isArrayType(dataType) { | ||
return dataType.endsWith('[]'); | ||
} | ||
function toElementType(dataType) { | ||
return dataType.slice(0, -2); | ||
} | ||
function toArrayType(dataType) { | ||
return dataType + '[]'; | ||
} | ||
function findSymbol(name) { | ||
@@ -604,0 +683,0 @@ for (var i = symbolTableStack.length - 1; i >= 0; i--) { |
{ | ||
"name": "introjs", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "IntroJS is a JavaScript implementation of the simplified programming language Intro.", | ||
@@ -5,0 +5,0 @@ "author": "introshu", |
@@ -10,5 +10,8 @@ # IntroJS | ||
var n = read_int() | ||
if n <= 0 | ||
return | ||
end | ||
while 1 | ||
var values = read_ints(n) | ||
if $values == 0 | ||
if !$values | ||
break | ||
@@ -23,3 +26,3 @@ end | ||
for v in values | ||
ret = ret + v | ||
ret += v | ||
end | ||
@@ -32,3 +35,3 @@ return ret | ||
Data types are only integer (Int) and its array (Int[]). | ||
Data types are only integer type (Int) and its array types (Int[], Int[][], ...). | ||
@@ -119,6 +122,2 @@ All codes must be written in a file. | ||
``` | ||
def new_ints(length: Int): Int[] | ||
``` | ||
``` | ||
def read(values: Int[], offset: Int, length: Int): Int | ||
@@ -149,3 +148,2 @@ ``` | ||
* Multidimensional array | ||
* Variable-length array | ||
@@ -152,0 +150,0 @@ * Associative array |
@@ -10,2 +10,3 @@ 'use strict'; | ||
'./examples/sum/sum.intro', | ||
'./test/arrays/arrays.intro', | ||
'./test/assignments/assignments.intro', | ||
@@ -12,0 +13,0 @@ './test/expressions/expressions.intro', |
0 | ||
-3 | ||
3 | ||
0 | ||
3 | ||
3 | ||
@@ -21,2 +26,4 @@ | ||
3 | ||
1 | ||
0 | ||
@@ -23,0 +30,0 @@ |
0 | ||
@@ -22,5 +21,1 @@ 10 | ||
2147483647 | ||
3 | ||
3 1 4 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
218828
78
6591
165