|
@@ -42,140 +42,6 @@ var HandleSymbolAsType = ContextType.HandleSymbolAsType;
|
|
HandleSymbolAsType.extend = Class.extend;
|
|
HandleSymbolAsType.extend = Class.extend;
|
|
HandleSymbolAsType.prototype.init = ContextType.HandleSymbolAsType;
|
|
HandleSymbolAsType.prototype.init = ContextType.HandleSymbolAsType;
|
|
|
|
|
|
-exports.emitEndStatement = function(context){
|
|
|
|
- context.codeGenerator().write(";\n");
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-exports.While = ChainedContext.extend({
|
|
|
|
- init: function WhileContext(context){
|
|
|
|
- ChainedContext.prototype.init.call(this, context);
|
|
|
|
- var gen = this.codeGenerator();
|
|
|
|
- gen.write("while (true)");
|
|
|
|
- gen.openScope();
|
|
|
|
- gen.write("if (");
|
|
|
|
- },
|
|
|
|
- handleExpression: function WhileContext$handleExpression(e){
|
|
|
|
- ContextIf.handleIfExpression(e);
|
|
|
|
- var gen = this.codeGenerator();
|
|
|
|
- gen.write(")");
|
|
|
|
- gen.openScope();
|
|
|
|
- },
|
|
|
|
- handleLiteral: function(s){
|
|
|
|
- if (s == "ELSIF"){
|
|
|
|
- var gen = this.codeGenerator();
|
|
|
|
- gen.closeScope("");
|
|
|
|
- gen.write("else if (");
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- endParse: function(){
|
|
|
|
- var gen = this.codeGenerator();
|
|
|
|
- gen.closeScope(" else break;\n");
|
|
|
|
- gen.closeScope("");
|
|
|
|
- }
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
-exports.Repeat = ChainedContext.extend({
|
|
|
|
- init: function RepeatContext(context){
|
|
|
|
- ChainedContext.prototype.init.call(this, context);
|
|
|
|
- var gen = context.codeGenerator();
|
|
|
|
- gen.write("do ");
|
|
|
|
- gen.openScope();
|
|
|
|
- }
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
-exports.Until = ChainedContext.extend({
|
|
|
|
- init: function UntilContext(context){
|
|
|
|
- ChainedContext.prototype.init.call(this, context);
|
|
|
|
- context.codeGenerator().closeScope(" while (");
|
|
|
|
- },
|
|
|
|
- codeGenerator: function(){ return nullCodeGenerator; },
|
|
|
|
- handleExpression: function(e){
|
|
|
|
- ContextIf.handleIfExpression(e);
|
|
|
|
- this.parent().codeGenerator().write( op.not(e).code() );
|
|
|
|
- },
|
|
|
|
- endParse: function(){
|
|
|
|
- this.parent().codeGenerator().write(");\n");
|
|
|
|
- }
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
-exports.For = ChainedContext.extend({
|
|
|
|
- init: function ForContext(context){
|
|
|
|
- ChainedContext.prototype.init.call(this, context);
|
|
|
|
- this.__var = undefined;
|
|
|
|
- this.__initExprParsed = false;
|
|
|
|
- this.__toExpr = CodeGenerator.makeSimpleGenerator();
|
|
|
|
- this.__toParsed = false;
|
|
|
|
- this.__by_parsed = false;
|
|
|
|
- this.__by = undefined;
|
|
|
|
- },
|
|
|
|
- handleIdent: function(id){
|
|
|
|
- var s = ContextHierarchy.getSymbol(this.root(), id);
|
|
|
|
- if (!s.isVariable())
|
|
|
|
- throw new Errors.Error("'" + s.id() + "' is not a variable");
|
|
|
|
- var type = s.info().type();
|
|
|
|
- if (type !== basicTypes.integer)
|
|
|
|
- throw new Errors.Error(
|
|
|
|
- "'" + s.id() + "' is a '"
|
|
|
|
- + type.description() + "' variable, 'FOR' control variable must be 'INTEGER'");
|
|
|
|
- this._handleInitCode(id, "for (" + id + " = ");
|
|
|
|
- },
|
|
|
|
- _handleInitCode: function(id, code){
|
|
|
|
- this.__var = id;
|
|
|
|
- this.codeGenerator().write(code);
|
|
|
|
- },
|
|
|
|
- _handleInitExpression: function(type){
|
|
|
|
- if (type != basicTypes.integer)
|
|
|
|
- throw new Errors.Error(
|
|
|
|
- "'INTEGER' expression expected to assign '" + this.__var
|
|
|
|
- + "', got '" + type.description() + "'");
|
|
|
|
- this.__initExprParsed = true;
|
|
|
|
- },
|
|
|
|
- handleExpression: function(e){
|
|
|
|
- var type = e.type();
|
|
|
|
- if (!this.__initExprParsed)
|
|
|
|
- this._handleInitExpression(type);
|
|
|
|
- else if (!this.__toParsed) {
|
|
|
|
- if (type != basicTypes.integer)
|
|
|
|
- throw new Errors.Error(
|
|
|
|
- "'INTEGER' expression expected as 'TO' parameter, got '" + type.description() + "'");
|
|
|
|
- this.__toParsed = true;
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- if (type != basicTypes.integer)
|
|
|
|
- throw new Errors.Error("'INTEGER' expression expected as 'BY' parameter, got '" + type.description() + "'");
|
|
|
|
- var value = e.constValue();
|
|
|
|
- if (!value)
|
|
|
|
- throw new Errors.Error("constant expression expected as 'BY' parameter");
|
|
|
|
- this.__by = value.value;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- codeGenerator: function(){
|
|
|
|
- if (this.__initExprParsed && !this.__toParsed)
|
|
|
|
- return this.__toExpr;
|
|
|
|
- if (this.__toParsed && !this.__by_parsed)
|
|
|
|
- return nullCodeGenerator; // suppress output for BY expression
|
|
|
|
-
|
|
|
|
- return this.parent().codeGenerator();
|
|
|
|
- },
|
|
|
|
- handleBegin: function(){
|
|
|
|
- this.__by_parsed = true;
|
|
|
|
-
|
|
|
|
- var relation = this.__by < 0 ? " >= " : " <= ";
|
|
|
|
- var step = this.__by === undefined
|
|
|
|
- ? "++" + this.__var
|
|
|
|
- : this.__var + (this.__by < 0
|
|
|
|
- ? " -= " + -this.__by
|
|
|
|
- : " += " + this.__by);
|
|
|
|
- var s = "; " + this.__var + relation + this.__toExpr.result() + "; " + step + ")";
|
|
|
|
- var gen = this.codeGenerator();
|
|
|
|
- gen.write(s);
|
|
|
|
- gen.openScope();
|
|
|
|
- },
|
|
|
|
- endParse: function(){this.codeGenerator().closeScope("");}
|
|
|
|
-});
|
|
|
|
-
|
|
|
|
-exports.emitForBegin = function(context){context.handleBegin();};
|
|
|
|
-
|
|
|
|
exports.CheckAssignment = ChainedContext.extend({
|
|
exports.CheckAssignment = ChainedContext.extend({
|
|
init: function Context$CheckAssignment(context){
|
|
init: function Context$CheckAssignment(context){
|
|
ChainedContext.prototype.init.call(this, context);
|
|
ChainedContext.prototype.init.call(this, context);
|