---------------------------------------------------------------- Source/grammar/Yylex.jflex ---------------------------------------------------------------- package grammar; import node.*; import java.io.*; import java_cup.runtime.*; %% %public %type Symbol %char %{ private int lineNumber = 1; public int lineNumber() { return lineNumber; } public Symbol token( int tokenType ) { System.err.println( "Obtain token " + sym.terminal_name( tokenType ) + " \"" + yytext() + "\"" ); return new Symbol( tokenType, yychar, yychar + yytext().length(), yytext() ); } %} charValue = \\.|[^\\\"] stringValue = \"{charValue}*\" intValue = [0-9]+ ident = [A-Za-z][A-Za-z0-9]* space = [\ \t] newline = \r|\n|\r\n %% {newline} { lineNumber++; } {space} { } ";" { return token( sym.SEMICOLON ); } "," { return token( sym.COMMA ); } "=" { return token( sym.ASSIGN ); } "||" { return token( sym.OR ); } "&&" { return token( sym.AND ); } "!" { return token( sym.NOT ); } "<" { return token( sym.LT ); } "<=" { return token( sym.LE ); } ">" { return token( sym.GT ); } ">=" { return token( sym.GE ); } "==" { return token( sym.EQ ); } "!=" { return token( sym.NE ); } "+" { return token( sym.PLUS ); } "-" { return token( sym.MINUS ); } "*" { return token( sym.TIMES ); } "/" { return token( sym.DIVIDE ); } "(" { return token( sym.LEFT ); } ")" { return token( sym.RIGHT ); } "{" { return token( sym.LEFTCURLY ); } "}" { return token( sym.RIGHTCURLY ); } "print" { return token( sym.PRINT ); } "if" { return token( sym.IF ); } "then" { return token( sym.THEN ); } "else" { return token( sym.ELSE ); } "while" { return token( sym.WHILE ); } "do" { return token( sym.DO ); } "for" { return token( sym.FOR ); } "to" { return token( sym.TO ); } "true" { return token( sym.TRUE ); } "false" { return token( sym.FALSE ); } {stringValue} { return token( sym.STRINGVALUE ); } {intValue} { return token( sym.INTVALUE ); } {ident} { return token( sym.IDENT ); } "//"[^\r\n]* { } "/*"~"*/" { } . { return token( sym.LEXERROR ); } <> { return token( sym.EOF ); } ---------------------------------------------------------------- Source/grammar/parser.cup ---------------------------------------------------------------- package grammar; import node.*; import node.stmtNode.*; import node.exprNode.*; import node.exprNode.valueNode.*; import node.exprNode.prefixNode.*; import node.exprNode.binaryNode.*; import node.exprNode.binaryNode.arithNode.*; import node.exprNode.binaryNode.boolNode.*; import node.exprNode.binaryNode.relationNode.*; import text.*; import java.io.*; import java_cup.runtime.*; parser code {: private Yylex lexer; private File file; public parser( File file ) { this(); this.file = file; try { lexer = new Yylex( new FileReader( file ) ); } catch ( IOException exception ) { throw new Error( "Unable to open file \"" + file + "\"" ); } } public String terminal_name( int id ) { return sym.terminal_name( id ); } public String non_terminal_name( int id ) { return sym.non_terminal_name( id ); } public String rule_name( int id ) { return sym.rule_name( id ); } public void report_error( String message, Object info ) { Print.error().println( file + " ( " + lexer.lineNumber() + " ): " + message ); try { if ( info instanceof Symbol ) { Symbol symbol = ( Symbol ) info; printText( symbol.left, symbol.right ); } } catch ( IOException e ) { } } private void printText( int left, int right ) throws IOException { Reader sourceReader = new FileReader( file ); int veryLeft = Math.max( left - 50, 0 ), veryRight = Math.min( right + 20, ( int ) file.length() ); char[] text = new char[ veryRight - veryLeft ]; char[] underline = new char[ veryRight - veryLeft ]; sourceReader.skip( veryLeft ); sourceReader.read( text ); for ( int i = 0; i < text.length; i++ ) { if ( text[ i ] < ' ' ) { text[ i ] = '|'; underline[ i ] = '|'; } else underline[ i ] = ' '; if ( left <= veryLeft + i && veryLeft + i < right ) underline[ i ] = '^'; } printLine( text ); printLine( underline ); } private static void printLine( char[] text ) { for ( int i = 0; i < text.length; i++ ) Print.error().print( text[ i ] ); Print.error().println(); } public void syntax_error( Symbol currToken ) { report_error( "Syntax Error", currToken ); } :}; scan with {: return lexer.yylex(); :}; terminal LEXERROR, OR, AND, NOT, LT, LE, GT, GE, EQ, NE, PLUS, MINUS, TIMES, DIVIDE, ASSIGN, PRINT, IF, THEN, ELSE, WHILE, DO, FOR, TO, SEMICOLON, LEFTCURLY, RIGHTCURLY, LEFT, RIGHT, COMMA, TRUE, FALSE; terminal String STRINGVALUE; terminal String INTVALUE; terminal String IDENT; nonterminal ProgramNode Program; nonterminal StmtListNode StmtList; nonterminal StmtNode Stmt; nonterminal ExprListNode ExprList; nonterminal ExprNode Expr, OrExpr, AndExpr, NotExpr, RelExpr, PlusExpr, MulExpr, Primary; start with Program; Program::= StmtList:stmtList {: RESULT = new ProgramNode( stmtList ); :} ; StmtList::= {: RESULT = new StmtListNode(); :} | StmtList:stmtList Stmt:stmt {: stmtList.addElement( stmt ); RESULT = stmtList; :} ; Stmt::= IDENT:ident ASSIGN Expr:expr SEMICOLON {: RESULT = new AssignStmtNode( ident, expr ); :} | PRINT LEFT ExprList:exprList RIGHT SEMICOLON {: RESULT = new PrintStmtNode( exprList ); :} | IF Expr:expr THEN Stmt:thenPart ELSE Stmt:elsePart {: RESULT = new IfThenElseStmtNode( expr, thenPart, elsePart ); :} | IF Expr:expr THEN Stmt:thenPart {: RESULT = new IfThenStmtNode( expr, thenPart ); :} | WHILE Expr:expr DO Stmt:stmt {: RESULT = new WhileStmtNode( expr, stmt ); :} | FOR IDENT:ident ASSIGN Expr:initExpr TO Expr:finalExpr DO Stmt:stmt {: RESULT = new ForStmtNode( ident, initExpr, finalExpr, stmt ); :} | LEFTCURLY StmtList:stmtList RIGHTCURLY {: RESULT = new CompoundStmtNode( stmtList ); :} | error SEMICOLON {: RESULT = new ErrorStmtNode(); :} | error RIGHTCURLY {: RESULT = new ErrorStmtNode(); :} ; ExprList::= Expr:expr {: RESULT = new ExprListNode( expr ); :} | ExprList:exprList COMMA Expr:expr {: exprList.addElement( expr ); RESULT = exprList; :} ; Expr::= OrExpr:expr {: RESULT = expr; :} ; OrExpr::= OrExpr:expr1 OR AndExpr:expr2 {: RESULT = new OrNode( expr1, expr2 ); :} | AndExpr:expr {: RESULT = expr; :} ; AndExpr::= AndExpr:expr1 AND NotExpr:expr2 {: RESULT = new AndNode( expr1, expr2 ); :} | NotExpr:expr {: RESULT = expr; :} ; NotExpr::= NOT NotExpr:expr2 {: RESULT = new NotNode( expr2 ); :} | RelExpr:expr {: RESULT = expr; :} ; RelExpr::= PlusExpr:expr1 LT PlusExpr:expr2 {: RESULT = new LessThanNode( expr1, expr2 ); :} | PlusExpr:expr1 LE PlusExpr:expr2 {: RESULT = new LessEqualNode( expr1, expr2 ); :} | PlusExpr:expr1 GT PlusExpr:expr2 {: RESULT = new GreaterThanNode( expr1, expr2 ); :} | PlusExpr:expr1 GE PlusExpr:expr2 {: RESULT = new GreaterEqualNode( expr1, expr2 ); :} | PlusExpr:expr1 EQ PlusExpr:expr2 {: RESULT = new EqualNode( expr1, expr2 ); :} | PlusExpr:expr1 NE PlusExpr:expr2 {: RESULT = new NotEqualNode( expr1, expr2 ); :} | PlusExpr:expr {: RESULT = expr; :} ; PlusExpr::= PlusExpr:expr PLUS MulExpr:expr2 {: RESULT = new PlusNode( expr, expr2 ); :} | PlusExpr:expr MINUS MulExpr:expr2 {: RESULT = new MinusNode( expr, expr2 ); :} | MINUS MulExpr:expr2 {: RESULT = new NegateNode( expr2 ); :} | MulExpr:expr {: RESULT = expr; :} ; MulExpr::= MulExpr:expr1 TIMES Primary:expr2 {: RESULT = new TimesNode( expr1, expr2 ); :} | MulExpr:expr1 DIVIDE Primary:expr2 {: RESULT = new DivideNode( expr1, expr2 ); :} | Primary:expr {: RESULT = expr; :} ; Primary::= LEFT Expr:expr RIGHT {: RESULT = expr; :} | TRUE {: RESULT = new BoolValueNode( true ); :} | FALSE {: RESULT = new BoolValueNode( false ); :} | INTVALUE:value {: RESULT = new IntValueNode( Integer.parseInt( value ) ); :} | STRINGVALUE:value {: RESULT = new StringValueNode( Convert.parseString( value.substring( 1, value.length() - 1 ) ) ); :} | IDENT:ident {: RESULT = new IdentNode( ident ); :} ; ---------------------------------------------------------------- Source/node/exprNode/valueNode/BoolValueNode.java ---------------------------------------------------------------- package node.exprNode.valueNode; import env.*; import code.*; import node.exprNode.*; public class BoolValueNode extends ValueNode { private boolean value; public BoolValueNode( boolean value ) { this.value = value; precedence = PREC_PRIMARY; } public String toString() { return "" + value; } public void evalCode( int freeReg ) { Code.instrn( "ldiq", Code.tempReg( freeReg ), Code.literal( value ) ); } } ---------------------------------------------------------------- Source/node/exprNode/binaryNode/boolNode/OrNode.java ---------------------------------------------------------------- package node.exprNode.binaryNode.boolNode; import node.exprNode.*; public class OrNode extends BoolNode { public OrNode( ExprNode left, ExprNode right ) { super( left, right ); precedence = PREC_OR; operator = "||"; opCode = "or"; } } ---------------------------------------------------------------- Source/node/exprNode/binaryNode/relationNode/NotRelationNode.java ---------------------------------------------------------------- package node.exprNode.binaryNode.relationNode; import code.*; import node.*; import node.exprNode.*; import node.exprNode.binaryNode.*; public abstract class NotRelationNode extends RelationNode { public NotRelationNode( ExprNode left, ExprNode right ) { super( left, right ); } public void evalCode( int freeReg ) { super.evalCode( freeReg ); Code.instrn( "cmpeq", Code.tempReg( freeReg ), "0" ); } } ---------------------------------------------------------------- Source/node/exprNode/binaryNode/relationNode/GreaterThanNode.java ---------------------------------------------------------------- package node.exprNode.binaryNode.relationNode; import node.exprNode.*; public class GreaterThanNode extends NotRelationNode { public GreaterThanNode( ExprNode left, ExprNode right ) { super( left, right ); operator = ">"; opCode = "cmple"; } } ---------------------------------------------------------------- Source/node/exprNode/prefixNode/NotNode.java ---------------------------------------------------------------- package node.exprNode.prefixNode; import code.*; import node.exprNode.*; public class NotNode extends PrefixNode { public NotNode( ExprNode right ) { super( right ); precedence = PREC_NOT; operator = "!"; } public void evalCode( int freeReg ) { right.evalCode( freeReg ); Code.instrn( "cmpeq", Code.tempReg( freeReg ), "0" ); } } ---------------------------------------------------------------- Source/node/stmtNode/IfThenStmtNode.java ---------------------------------------------------------------- package node.stmtNode; import env.*; import code.*; import node.exprNode.*; public class IfThenStmtNode extends StmtNode { private ExprNode cond; private StmtNode thenPart; public IfThenStmtNode( ExprNode cond, StmtNode thenPart ) { this.cond = cond; this.thenPart = thenPart; } public String toString() { return "if " + cond + " then%+%n" + thenPart + "%-"; } public void genDeclCode( Env env ) { thenPart.genDeclCode( env ); } public void genCode() { Code.enter(); Code.labelDefn( "if" ); cond.evalCode( 0 ); Code.instrn( "blbc", "$t0", "end" ); Code.labelDefn( "then" ); thenPart.genCode(); Code.labelDefn( "end" ); Code.exit(); } } ---------------------------------------------------------------- Source/node/stmtNode/IfThenElseStmtNode.java ---------------------------------------------------------------- package node.stmtNode; import env.*; import code.*; import node.exprNode.*; public class IfThenElseStmtNode extends StmtNode { private ExprNode cond; private StmtNode thenPart; private StmtNode elsePart; public IfThenElseStmtNode( ExprNode cond, StmtNode thenPart, StmtNode elsePart ) { this.cond = cond; this.thenPart = thenPart; this.elsePart = elsePart; } public String toString() { return "if " + cond + " then%+%n" + thenPart + "%-%nelse%+%n" + elsePart + "%-"; } public void genDeclCode( Env env ) { thenPart.genDeclCode( env ); elsePart.genDeclCode( env ); } public void genCode() { Code.enter(); Code.labelDefn( "if" ); cond.evalCode( 0 ); Code.instrn( "blbc", "$t0", "else" ); Code.labelDefn( "then" ); thenPart.genCode(); Code.instrn( "br", "end" ); Code.labelDefn( "else" ); elsePart.genCode(); Code.labelDefn( "end" ); Code.exit(); } } ---------------------------------------------------------------- Source/node/stmtNode/ForStmtNode.java ---------------------------------------------------------------- package node.stmtNode; import env.*; import code.*; import node.exprNode.*; public class ForStmtNode extends StmtNode { private String ident; private ExprNode initExpr; private ExprNode finalExpr; private StmtNode stmt; public ForStmtNode( String ident, ExprNode initExpr, ExprNode finalExpr, StmtNode stmt ) { this.ident = ident; this.initExpr = initExpr; this.finalExpr = finalExpr; this.stmt = stmt; } public String toString() { return "for " + ident + " = " + initExpr + " to " + finalExpr + " do%+%n" + stmt + "%-"; } public void genDeclCode( Env env ) { env.add( ident ); stmt.genDeclCode( env ); } public void genCode() { Code.enter(); Code.labelDefn( "for" ); initExpr.evalCode( 0 ); Code.instrn( "ldiq", "$t1", ident ); Code.instrn( "stq", "$t0", "($t1)" ); Code.labelDefn( "while" ); Code.instrn( "ldiq", "$t0", ident ); Code.instrn( "ldq", "$t0", "($t0)" ); finalExpr.evalCode( 1 ); Code.instrn( "cmple", "$t0", "$t1" ); Code.instrn( "blbc", "$t0", "end" ); Code.labelDefn( "do" ); stmt.genCode(); Code.labelDefn( "increment" ); Code.instrn( "ldiq", "$t0", ident ); Code.instrn( "ldq", "$t1", "($t0)" ); Code.instrn( "addq", "$t1", "1" ); Code.instrn( "stq", "$t1", "($t0)" ); Code.instrn( "br", "while" ); Code.labelDefn( "end" ); Code.exit(); } } ---------------------------------------------------------------- jflex.error ---------------------------------------------------------------- Reading "Source/grammar/Yylex.jflex" Constructing NFA : 159 states in NFA Converting NFA to DFA : ........................................................................... 77 states before minimization, 69 states in minimized DFA Writing code to "Source/grammar/Yylex.java" ---------------------------------------------------------------- cup.error ---------------------------------------------------------------- Opening files... Parsing specification from standard input... Checking specification... Warning: Terminal "LEXERROR" was declared but never used Building parse tables... Computing non-terminal nullability... Computing first sets... Building state machine... Filling in tables... *** Shift/Reduce conflict found in state #53 between Stmt ::= IF Expr THEN Stmt (*) and Stmt ::= IF Expr THEN Stmt (*) ELSE Stmt under symbol ELSE Resolved in favor of shifting. Checking for non-reduced productions... Writing parser... Closing files... ------- CUP v0.10k Parser Generation Summary ------- 0 errors and 2 warnings 36 terminals, 13 non-terminals, and 42 productions declared, producing 84 unique parse states. 1 terminal declared but not used. 0 non-terminal declared but not used. 0 productions never reduced. 1 conflict detected (1 expected). Code written to "parser.java", and "sym.java". ---------------------------------------------------- (v0.10k) ---------------------------------------------------------------- javac.error ---------------------------------------------------------------- ---------------------------------------------------------------- TestPrograms/test.1.boolLit/program.in ---------------------------------------------------------------- print( "true = %d, false = %d\n", true, false ); ---------------------------------------------------------------- TestPrograms/test.1.boolLit/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.1.boolLit/program.print ---------------------------------------------------------------- print( "true = %d, false = %d\n", true, false ); ---------------------------------------------------------------- TestPrograms/test.1.boolLit/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { } data code { public enter: { { const { string: asciiz "true = %d, false = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, 0; mov $t0, $a2; bsr IO.printf.enter; } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.1.boolLit/sim.out ---------------------------------------------------------------- true = 1, false = 0 ---------------------------------------------------------------- TestPrograms/test.2.a.boolOpr/program.in ---------------------------------------------------------------- a = 0; b = 1; print( "%d = %d\n", true, true || false && true ); print( "%d = %d\n", true, false && true || true ); print( "%d = %d\n", true, ( false || true ) && true ); print( "%d = %d\n", false, false || ( true && false ) ); print( "%d = %d\n", true, ( false && true ) || true ); print( "%d = %d\n", false, false && ( true || true ) ); print( "%d = %d\n", false, ! a < b ); print( "%d = %d\n", true, ! b < a ); print( "%d = %d\n", false, ! ( a < b ) ); print( "%d = %d\n", false, ( ! a ) < b ); ---------------------------------------------------------------- TestPrograms/test.2.a.boolOpr/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.2.a.boolOpr/program.print ---------------------------------------------------------------- a = 0; b = 1; print( "%d = %d\n", true, true || false && true ); print( "%d = %d\n", true, false && true || true ); print( "%d = %d\n", true, ( false || true ) && true ); print( "%d = %d\n", false, false || true && false ); print( "%d = %d\n", true, false && true || true ); print( "%d = %d\n", false, false && ( true || true ) ); print( "%d = %d\n", false, ! a < b ); print( "%d = %d\n", true, ! b < a ); print( "%d = %d\n", false, ! a < b ); print( "%d = %d\n", false, ( ! a ) < b ); ---------------------------------------------------------------- TestPrograms/test.2.a.boolOpr/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { a: quad 0; b: quad 0; } data code { public enter: { ldiq $t0, a; ldiq $t1, 0; stq $t1, ($t0); } { ldiq $t0, b; ldiq $t1, 1; stq $t1, ($t0); } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, 1; ldiq $t1, 0; ldiq $t2, 1; and $t1, $t2; or $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, 0; ldiq $t1, 1; and $t0, $t1; ldiq $t1, 1; or $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, 0; ldiq $t1, 1; or $t0, $t1; ldiq $t1, 1; and $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 0; mov $t0, $a1; ldiq $t0, 0; ldiq $t1, 1; ldiq $t2, 0; and $t1, $t2; or $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, 0; ldiq $t1, 1; and $t0, $t1; ldiq $t1, 1; or $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 0; mov $t0, $a1; ldiq $t0, 0; ldiq $t1, 1; ldiq $t2, 1; or $t1, $t2; and $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 0; mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; cmpeq $t0, 0; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 1; mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); ldiq $t1, a; ldq $t1, ($t1); cmplt $t0, $t1; cmpeq $t0, 0; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 0; mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; cmpeq $t0, 0; mov $t0, $a2; bsr IO.printf.enter; } { { const { string: asciiz "%d = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, 0; mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); cmpeq $t0, 0; ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; mov $t0, $a2; bsr IO.printf.enter; } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.2.a.boolOpr/sim.out ---------------------------------------------------------------- 1 = 1 1 = 1 1 = 1 0 = 0 1 = 1 0 = 0 0 = 0 1 = 1 0 = 0 0 = 0 ---------------------------------------------------------------- TestPrograms/test.2.b.relation/program.in ---------------------------------------------------------------- a = 3; b = 4; print( "%d < %d is %d\n", a, b, a < b ); print( "%d <= %d is %d\n", a, b, a <= b ); print( "%d > %d is %d\n", a, b, a > b ); print( "%d >= %d is %d\n", a, b, a >= b ); print( "%d == %d is %d\n", a, b, a == b ); print( "%d != %d is %d\n", a, b, a != b ); a = true; b = false; print( "%d || %d is %d\n", a, b, a || b ); print( "%d && %d is %d\n", a, b, a && b ); print( "! %d is %d\n", a, ! a ); ---------------------------------------------------------------- TestPrograms/test.2.b.relation/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.2.b.relation/program.print ---------------------------------------------------------------- a = 3; b = 4; print( "%d < %d is %d\n", a, b, a < b ); print( "%d <= %d is %d\n", a, b, a <= b ); print( "%d > %d is %d\n", a, b, a > b ); print( "%d >= %d is %d\n", a, b, a >= b ); print( "%d == %d is %d\n", a, b, a == b ); print( "%d != %d is %d\n", a, b, a != b ); a = true; b = false; print( "%d || %d is %d\n", a, b, a || b ); print( "%d && %d is %d\n", a, b, a && b ); print( "! %d is %d\n", a, ! a ); ---------------------------------------------------------------- TestPrograms/test.2.b.relation/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { a: quad 0; b: quad 0; } data code { public enter: { ldiq $t0, a; ldiq $t1, 3; stq $t1, ($t0); } { ldiq $t0, b; ldiq $t1, 4; stq $t1, ($t0); } { { const { string: asciiz "%d < %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d <= %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d > %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; cmpeq $t0, 0; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d >= %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; cmpeq $t0, 0; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d == %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmpeq $t0, $t1; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d != %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmpeq $t0, $t1; cmpeq $t0, 0; mov $t0, $a3; bsr IO.printf.enter; } { ldiq $t0, a; ldiq $t1, 1; stq $t1, ($t0); } { ldiq $t0, b; ldiq $t1, 0; stq $t1, ($t0); } { { const { string: asciiz "%d || %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); or $t0, $t1; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "%d && %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); and $t0, $t1; mov $t0, $a3; bsr IO.printf.enter; } { { const { string: asciiz "! %d is %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); cmpeq $t0, 0; mov $t0, $a2; bsr IO.printf.enter; } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.2.b.relation/sim.out ---------------------------------------------------------------- 3 < 4 is 1 3 <= 4 is 1 3 > 4 is 0 3 >= 4 is 0 3 == 4 is 0 3 != 4 is 1 1 || 0 is 1 1 && 0 is 0 ! 1 is 0 ---------------------------------------------------------------- TestPrograms/test.3.a.ifThen/program.in ---------------------------------------------------------------- a = 3; b = 4; if a < b then print( "%d < %d\n", a, b ); if a <= b then print( "%d <= %d\n", a, b ); if a == b then print( "%d == %d\n", a, b ); if a > b then print( "%d > %d\n", a, b ); c = 4; d = 3; if c < d then print( "%d < %d\n", c, d ); if c <= d then print( "%d <= %d\n", c, d ); if c == d then print( "%d == %d\n", c, d ); if c > d then print( "%d > %d\n", c, d ); ---------------------------------------------------------------- TestPrograms/test.3.a.ifThen/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.3.a.ifThen/program.print ---------------------------------------------------------------- a = 3; b = 4; if a < b then print( "%d < %d\n", a, b ); if a <= b then print( "%d <= %d\n", a, b ); if a == b then print( "%d == %d\n", a, b ); if a > b then print( "%d > %d\n", a, b ); c = 4; d = 3; if c < d then print( "%d < %d\n", c, d ); if c <= d then print( "%d <= %d\n", c, d ); if c == d then print( "%d == %d\n", c, d ); if c > d then print( "%d > %d\n", c, d ); ---------------------------------------------------------------- TestPrograms/test.3.a.ifThen/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { a: quad 0; b: quad 0; c: quad 0; d: quad 0; } data code { public enter: { ldiq $t0, a; ldiq $t1, 3; stq $t1, ($t0); } { ldiq $t0, b; ldiq $t1, 4; stq $t1, ($t0); } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d < %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d <= %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmpeq $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d == %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; cmpeq $t0, 0; blbc $t0, end; then: { { const { string: asciiz "%d > %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { ldiq $t0, c; ldiq $t1, 4; stq $t1, ($t0); } { ldiq $t0, d; ldiq $t1, 3; stq $t1, ($t0); } { if: ldiq $t0, c; ldq $t0, ($t0); ldiq $t1, d; ldq $t1, ($t1); cmplt $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d < %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, c; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, d; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, c; ldq $t0, ($t0); ldiq $t1, d; ldq $t1, ($t1); cmple $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d <= %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, c; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, d; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, c; ldq $t0, ($t0); ldiq $t1, d; ldq $t1, ($t1); cmpeq $t0, $t1; blbc $t0, end; then: { { const { string: asciiz "%d == %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, c; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, d; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, c; ldq $t0, ($t0); ldiq $t1, d; ldq $t1, ($t1); cmple $t0, $t1; cmpeq $t0, 0; blbc $t0, end; then: { { const { string: asciiz "%d > %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, c; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, d; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.3.a.ifThen/sim.out ---------------------------------------------------------------- 3 < 4 3 <= 4 4 > 3 ---------------------------------------------------------------- TestPrograms/test.3.b.ifElse/program.in ---------------------------------------------------------------- a = 3; b = 4; if a < b then print( "%d < %d is true\n", a, b ); else print( "%d < %d is false\n", a, b ); if a == b then print( "%d == %d is true\n", a, b ); else print( "%d == %d is false\n", a, b ); if a == a then print( "%d == %d is true\n", a, a ); else print( "%d == %d is false\n", a, a ); if a <= b then print( "%d <= %d is true\n", a, b ); else print( "%d <= %d is false\n", a, b ); if a > b then print( "%d > %d is true\n", a, b ); else print( "%d > %d is false\n", a, b ); ---------------------------------------------------------------- TestPrograms/test.3.b.ifElse/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.3.b.ifElse/program.print ---------------------------------------------------------------- a = 3; b = 4; if a < b then print( "%d < %d is true\n", a, b ); else print( "%d < %d is false\n", a, b ); if a == b then print( "%d == %d is true\n", a, b ); else print( "%d == %d is false\n", a, b ); if a == a then print( "%d == %d is true\n", a, a ); else print( "%d == %d is false\n", a, a ); if a <= b then print( "%d <= %d is true\n", a, b ); else print( "%d <= %d is false\n", a, b ); if a > b then print( "%d > %d is true\n", a, b ); else print( "%d > %d is false\n", a, b ); ---------------------------------------------------------------- TestPrograms/test.3.b.ifElse/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { a: quad 0; b: quad 0; } data code { public enter: { ldiq $t0, a; ldiq $t1, 3; stq $t1, ($t0); } { ldiq $t0, b; ldiq $t1, 4; stq $t1, ($t0); } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmplt $t0, $t1; blbc $t0, else; then: { { const { string: asciiz "%d < %d is true\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d < %d is false\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmpeq $t0, $t1; blbc $t0, else; then: { { const { string: asciiz "%d == %d is true\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d == %d is false\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, a; ldq $t1, ($t1); cmpeq $t0, $t1; blbc $t0, else; then: { { const { string: asciiz "%d == %d is true\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d == %d is false\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; blbc $t0, else; then: { { const { string: asciiz "%d <= %d is true\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d <= %d is false\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } { if: ldiq $t0, a; ldq $t0, ($t0); ldiq $t1, b; ldq $t1, ($t1); cmple $t0, $t1; cmpeq $t0, 0; blbc $t0, else; then: { { const { string: asciiz "%d > %d is true\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d > %d is false\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } end: } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.3.b.ifElse/sim.out ---------------------------------------------------------------- 3 < 4 is true 3 == 4 is false 3 == 3 is true 3 <= 4 is true 3 > 4 is false ---------------------------------------------------------------- TestPrograms/test.3.c.ifElse/program.in ---------------------------------------------------------------- if false then a = 3; else b = 4; print( "a = %d, b = %d\n", a, b ); ---------------------------------------------------------------- TestPrograms/test.3.c.ifElse/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.3.c.ifElse/program.print ---------------------------------------------------------------- if false then a = 3; else b = 4; print( "a = %d, b = %d\n", a, b ); ---------------------------------------------------------------- TestPrograms/test.3.c.ifElse/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { a: quad 0; b: quad 0; } data code { public enter: { if: ldiq $t0, 0; blbc $t0, else; then: { ldiq $t0, a; ldiq $t1, 3; stq $t1, ($t0); } br end; else: { ldiq $t0, b; ldiq $t1, 4; stq $t1, ($t0); } end: } { { const { string: asciiz "a = %d, b = %d\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, a; ldq $t0, ($t0); mov $t0, $a1; ldiq $t0, b; ldq $t0, ($t0); mov $t0, $a2; bsr IO.printf.enter; } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.3.c.ifElse/sim.out ---------------------------------------------------------------- a = 0, b = 4 ---------------------------------------------------------------- TestPrograms/test.4.for/program.in ---------------------------------------------------------------- low = 10; high = 20; for i = low to high do if i / 2 * 2 == i then print( "%d is even\n", i ); else print( "%d is odd\n", i ); ---------------------------------------------------------------- TestPrograms/test.4.for/program.err ---------------------------------------------------------------- Reprinting ... Generate Code ... ---------------------------------------------------------------- TestPrograms/test.4.for/program.print ---------------------------------------------------------------- low = 10; high = 20; for i = low to high do if i / 2 * 2 == i then print( "%d is even\n", i ); else print( "%d is odd\n", i ); ---------------------------------------------------------------- TestPrograms/test.4.for/usercode.user.s ---------------------------------------------------------------- entry main.enter; import "../../IMPORT/callsys.h"; import "../../IMPORT/proc.h"; import "../../IMPORT/callsys.lib.s"; import "../../IMPORT/string.lib.s"; import "../../IMPORT/number.lib.s"; import "../../IMPORT/io.lib.s"; public block main { data { low: quad 0; high: quad 0; i: quad 0; } data code { public enter: { ldiq $t0, low; ldiq $t1, 10; stq $t1, ($t0); } { ldiq $t0, high; ldiq $t1, 20; stq $t1, ($t0); } { for: ldiq $t0, low; ldq $t0, ($t0); ldiq $t1, i; stq $t0, ($t1); while: ldiq $t0, i; ldq $t0, ($t0); ldiq $t1, high; ldq $t1, ($t1); cmple $t0, $t1; blbc $t0, end; do: { if: ldiq $t0, i; ldq $t0, ($t0); ldiq $t1, 2; divq $t0, $t1; ldiq $t1, 2; mulq $t0, $t1; ldiq $t1, i; ldq $t1, ($t1); cmpeq $t0, $t1; blbc $t0, else; then: { { const { string: asciiz "%d is even\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, i; ldq $t0, ($t0); mov $t0, $a1; bsr IO.printf.enter; } br end; else: { { const { string: asciiz "%d is odd\n"; align; } const ldiq $t0, string; } mov $t0, $a0; ldiq $t0, i; ldq $t0, ($t0); mov $t0, $a1; bsr IO.printf.enter; } end: } increment: ldiq $t0, i; ldq $t1, ($t0); addq $t1, 1; stq $t1, ($t0); br while; end: } clr $a0; bsr Sys.exit.enter; } code } block main ---------------------------------------------------------------- TestPrograms/test.4.for/sim.out ---------------------------------------------------------------- 10 is even 11 is odd 12 is even 13 is odd 14 is even 15 is odd 16 is even 17 is odd 18 is even 19 is odd 20 is even