├── README ├── src ├── Tiger.java ├── control │ ├── CommandLine.java │ └── Control.java ├── lexer │ ├── Lexer.java │ └── Token.java ├── parser │ └── Parser.java ├── slp │ ├── Compiler.java │ ├── Interpreter.java │ ├── Main.java │ ├── MaxArgument.java │ ├── PrettyPrint.java │ ├── SamplePrograms.java │ └── Slp.java └── util │ ├── Error.java │ └── Todo.java └── test ├── BinarySearch.java ├── BinaryTree.java ├── BubbleSort.java ├── Factorial.java ├── LinearSearch.java ├── LinkedList.java ├── QuickSort.java ├── Sum-infinite.java ├── Sum.java └── TreeVisitor.java /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bjhua/tiger/f51cab58bdb765b9fb9e3e064aefa128d40d3b55/README -------------------------------------------------------------------------------- /src/Tiger.java: -------------------------------------------------------------------------------- 1 | import control.CommandLine; 2 | import parser.Parser; 3 | 4 | // the Tiger compiler main class. 5 | public class Tiger { 6 | public static void main(String[] args) throws Exception { 7 | Parser parser; 8 | 9 | // /////////////////////////////////////////////////////// 10 | // process command line arguments 11 | CommandLine cmd = new CommandLine(); 12 | // get the file to be compiled 13 | String fileName = cmd.scan(args); 14 | if (fileName == null) { 15 | // no input file is given, then exit silently. 16 | return; 17 | } 18 | 19 | // ///////////////////////////////////////////////////////// 20 | // otherwise, we continue the normal compilation pipeline. 21 | // first, create a parser: 22 | parser = new Parser(fileName); 23 | // then use it to parse the input file: 24 | parser.parse(); 25 | 26 | } 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/control/CommandLine.java: -------------------------------------------------------------------------------- 1 | package control; 2 | 3 | import java.util.List; 4 | import java.util.function.Consumer; 5 | 6 | public class CommandLine { 7 | // alphabetically-ordered 8 | enum Kind { 9 | Bool, 10 | Empty, 11 | Int, 12 | String, 13 | StringList, 14 | } 15 | 16 | record Arg( 17 | String name, 18 | String option, 19 | String description, 20 | Kind kind, 21 | Consumer action) { 22 | } 23 | 24 | private final List args; 25 | 26 | public void error(String message) { 27 | System.err.println("Error: " + message); 28 | usage(); 29 | System.exit(1); 30 | } 31 | 32 | public CommandLine() { 33 | this.args = List.of( 34 | new Arg("dump", 35 | "{token}", 36 | "dump tokens from lexical analysis", 37 | Kind.String, 38 | (Object x) -> { 39 | switch ((String) x) { 40 | case "token" -> Control.Lexer.dumpToken = true; 41 | default -> error("unknown argument: " + x); 42 | } 43 | }), 44 | new Arg( 45 | "help", 46 | null, 47 | "show this help information", 48 | Kind.Empty, 49 | (_) -> { 50 | usage(); 51 | System.exit(1); 52 | }) 53 | ); 54 | } 55 | 56 | // scan the command line arguments, return the file name 57 | // in it; return null if there is no file name. 58 | // The file name should be unique. 59 | public String scan(String[] cmdLineArgs) { 60 | String filename = null; 61 | 62 | for (int i = 0; i < cmdLineArgs.length; i++) { 63 | String cmdArg = cmdLineArgs[i]; 64 | if (!cmdArg.startsWith("-")) { 65 | if (null != filename) { 66 | error("compile only one Java file one time"); 67 | } else { 68 | filename = cmdArg; 69 | continue; 70 | } 71 | } 72 | 73 | // to crawl through arguments: 74 | cmdArg = cmdArg.substring(1); 75 | boolean foundArg = false; 76 | for (Arg arg : this.args) { 77 | if (!arg.name.equals(cmdArg)) 78 | continue; 79 | 80 | foundArg = true; 81 | String param = ""; 82 | switch (arg.kind) { 83 | case Kind.Empty -> arg.action.accept(null); 84 | default -> { 85 | i++; 86 | if (i >= cmdLineArgs.length) 87 | error("wants more arguments"); 88 | else { 89 | param = cmdLineArgs[i]; 90 | } 91 | } 92 | } 93 | switch (arg.kind) { 94 | case Kind.Empty -> arg.action.accept(null); 95 | case Kind.Bool -> { 96 | switch (param) { 97 | case "true" -> arg.action.accept(true); 98 | case "false" -> arg.action.accept(false); 99 | default -> error(arg.name + " requires a boolean"); 100 | } 101 | } 102 | case Int -> { 103 | int num = 0; 104 | try { 105 | num = Integer.parseInt(param); 106 | } catch (java.lang.NumberFormatException e) { 107 | error(arg.name + " requires an integer"); 108 | } 109 | arg.action.accept(num); 110 | } 111 | case String -> arg.action.accept(param); 112 | case StringList -> { 113 | String[] strArray = param.split(","); 114 | arg.action.accept(strArray); 115 | } 116 | default -> error("bad argument"); 117 | } 118 | } 119 | if (!foundArg) { 120 | error("invalid option: " + cmdLineArgs[i]); 121 | } 122 | } 123 | return filename; 124 | } 125 | 126 | private void outputSpace(int n) { 127 | if (n < 0) 128 | throw new util.Error(); 129 | 130 | while (n-- != 0) 131 | System.out.print(" "); 132 | } 133 | 134 | public void output() { 135 | int max = 0; 136 | for (Arg a : this.args) { 137 | int current = a.name.length(); 138 | if (a.option != null) 139 | current += a.option.length(); 140 | if (current > max) 141 | max = current; 142 | } 143 | System.out.println("Available options:"); 144 | for (Arg a : this.args) { 145 | int current = a.name.length(); 146 | System.out.print(" -" + a.name); 147 | if (a.option != null) { 148 | current += a.option.length(); 149 | System.out.print(a.option); 150 | } 151 | try { 152 | outputSpace(max - current + 1); 153 | } catch (Exception e) { 154 | throw new util.Error(e); 155 | } 156 | System.out.println(a.description); 157 | } 158 | } 159 | 160 | public void usage() { 161 | final int startYear = 2013; 162 | System.out.println( 163 | "The Tiger compiler. Copyright (C) " + startYear + "-, SSE of USTC.\n" + 164 | "Usage: java Tiger [options] \n"); 165 | output(); 166 | } 167 | } 168 | 169 | -------------------------------------------------------------------------------- /src/control/Control.java: -------------------------------------------------------------------------------- 1 | package control; 2 | 3 | public class Control { 4 | // the lexer 5 | public static class Lexer { 6 | public static boolean dumpToken = false; 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /src/lexer/Lexer.java: -------------------------------------------------------------------------------- 1 | package lexer; 2 | 3 | import util.Todo; 4 | 5 | import java.io.InputStream; 6 | 7 | import static control.Control.Lexer.dumpToken; 8 | 9 | public record Lexer(String fileName, 10 | InputStream fileStream) { 11 | 12 | // When called, return the next token (refer to the code "Token.java") 13 | // from the input stream. 14 | // Return TOKEN_EOF when reaching the end of the input stream. 15 | private Token nextToken0() throws Exception { 16 | int c = this.fileStream.read(); 17 | // skip all kinds of "blanks" 18 | // think carefully about how to set up "colNum" and "rowNum" correctly? 19 | while (' ' == c || '\t' == c || '\n' == c) { 20 | c = this.fileStream.read(); 21 | } 22 | switch (c) { 23 | case -1 -> { 24 | // The value for "lineNum" is now "null", 25 | // you should modify this to an appropriate 26 | // line number for the "EOF" token. 27 | return new Token(Token.Kind.EOF, null, null); 28 | } 29 | case '+' -> { 30 | return new Token(Token.Kind.ADD, null, null); 31 | } 32 | case ',' -> { 33 | return new Token(Token.Kind.COMMA, null, null); 34 | } 35 | default -> { 36 | // Lab 1, exercise 9: supply missing code to 37 | // recognize other kind of tokens. 38 | // Hint: think carefully about the basic 39 | // data structure and algorithms. The code 40 | // is not that much and may be less than 50 lines. 41 | // If you find you are writing a lot of code, you 42 | // are on the wrong way. 43 | throw new Todo(c); 44 | } 45 | } 46 | } 47 | 48 | public Token nextToken() { 49 | Token t = null; 50 | 51 | try { 52 | t = this.nextToken0(); 53 | } catch (Exception e) { 54 | //e.printStackTrace(); 55 | System.exit(1); 56 | } 57 | if (dumpToken) { 58 | System.out.println(t); 59 | } 60 | return t; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/lexer/Token.java: -------------------------------------------------------------------------------- 1 | package lexer; 2 | 3 | import java.util.Optional; 4 | 5 | // Lab 1, exercise 8: read the MiniJava specification carefully, 6 | // and fill in other possible tokens. 7 | public class Token { 8 | // alphabetically ordered 9 | public enum Kind { 10 | ADD, 11 | CLASS, 12 | COMMA, 13 | DOT, 14 | EOF, 15 | ID, 16 | INT, 17 | LBRACKET, 18 | LENGTH, 19 | LPAREN, 20 | NEW, 21 | RBRACKET, 22 | RPAREN, 23 | SEMICOLON, 24 | } 25 | 26 | // kind of the token 27 | public Kind kind; 28 | // extra lexeme for this token, if any 29 | public Optional lexeme; 30 | // position of the token in the source file: (row, column) 31 | public Integer rowNum; 32 | public Integer colNum; 33 | 34 | 35 | public Token(Kind kind, 36 | Integer rowNum, 37 | Integer colNum) { 38 | this.kind = kind; 39 | this.lexeme = Optional.empty(); 40 | this.rowNum = rowNum; 41 | this.colNum = colNum; 42 | } 43 | 44 | public Token(Kind kind, 45 | String lexeme, 46 | Integer rowNum, 47 | Integer colNum) { 48 | this.kind = kind; 49 | this.lexeme = Optional.of(lexeme); 50 | this.rowNum = rowNum; 51 | this.colNum = colNum; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | String s; 57 | 58 | s = ": " + (this.lexeme.orElse("")) + 59 | ": at row " + (this.rowNum == null ? "" : rowNum.toString()) + 60 | ": at column " + (this.colNum == null ? "" : colNum.toString()); 61 | return this.kind + s; 62 | } 63 | } -------------------------------------------------------------------------------- /src/parser/Parser.java: -------------------------------------------------------------------------------- 1 | package parser; 2 | 3 | import lexer.Lexer; 4 | import lexer.Token; 5 | import util.Todo; 6 | 7 | import java.io.BufferedInputStream; 8 | import java.io.FileInputStream; 9 | 10 | import static java.lang.System.exit; 11 | 12 | public class Parser { 13 | String inputFileName; 14 | BufferedInputStream inputStream; 15 | Lexer lexer; 16 | Token current; 17 | 18 | public Parser(String fileName) { 19 | this.inputFileName = fileName; 20 | } 21 | 22 | // ///////////////////////////////////////////// 23 | // utility methods to connect the lexer and the parser. 24 | private void advance() { 25 | current = lexer.nextToken(); 26 | } 27 | 28 | private void eatToken(Token.Kind kind) { 29 | if (kind.equals(current.kind)) { 30 | advance(); 31 | return; 32 | } 33 | System.out.println("Expects: " + kind); 34 | System.out.println("But got: " + current.kind); 35 | error("syntax error"); 36 | } 37 | 38 | private void error(String errMsg) { 39 | System.out.println("Error: " + errMsg + ", compilation aborting...\n"); 40 | exit(1); 41 | } 42 | 43 | // //////////////////////////////////////////////////////////// 44 | // The followings are methods for parsing. 45 | 46 | // A bunch of parsing methods to parse expressions. 47 | // The messy parts are to deal with precedence and associativity. 48 | 49 | // ExpList -> Exp ExpRest* 50 | // -> 51 | // ExpRest -> , Exp 52 | private void parseExpList() { 53 | if (current.kind.equals(Token.Kind.RPAREN)) 54 | return; 55 | parseExp(); 56 | while (current.kind.equals(Token.Kind.COMMA)) { 57 | advance(); 58 | parseExp(); 59 | } 60 | return; 61 | } 62 | 63 | // AtomExp -> (exp) 64 | // -> INTEGER_LITERAL 65 | // -> true 66 | // -> false 67 | // -> this 68 | // -> id 69 | // -> new int [exp] 70 | // -> new id () 71 | private void parseAtomExp() { 72 | switch (current.kind) { 73 | case LPAREN: 74 | advance(); 75 | parseExp(); 76 | eatToken(Token.Kind.RPAREN); 77 | return; 78 | case ID: 79 | advance(); 80 | return; 81 | case NEW: { 82 | advance(); 83 | switch (current.kind) { 84 | case INT: 85 | advance(); 86 | eatToken(Token.Kind.LBRACKET); 87 | parseExp(); 88 | eatToken(Token.Kind.RBRACKET); 89 | return; 90 | case ID: 91 | advance(); 92 | eatToken(Token.Kind.LPAREN); 93 | eatToken(Token.Kind.RPAREN); 94 | return; 95 | default: 96 | throw new Todo(); 97 | } 98 | } 99 | default: 100 | throw new Todo(); 101 | } 102 | } 103 | 104 | // NotExp -> AtomExp 105 | // -> AtomExp .id (expList) 106 | // -> AtomExp [exp] 107 | // -> AtomExp .length 108 | private void parseNotExp() { 109 | parseAtomExp(); 110 | while (current.kind.equals(Token.Kind.DOT) || 111 | current.kind.equals(Token.Kind.LBRACKET)) { 112 | if (current.kind.equals(Token.Kind.DOT)) { 113 | advance(); 114 | if (current.kind.equals(Token.Kind.LENGTH)) { 115 | advance(); 116 | return; 117 | } 118 | eatToken(Token.Kind.ID); 119 | eatToken(Token.Kind.LPAREN); 120 | parseExpList(); 121 | eatToken(Token.Kind.RPAREN); 122 | } else { 123 | advance(); 124 | parseExp(); 125 | eatToken(Token.Kind.RBRACKET); 126 | } 127 | } 128 | return; 129 | } 130 | 131 | // TimesExp -> ! TimesExp 132 | // -> NotExp 133 | private void parseTimesExp() { 134 | throw new Todo(); 135 | 136 | } 137 | 138 | // AddSubExp -> TimesExp * TimesExp 139 | // -> TimesExp 140 | private void parseAddSubExp() { 141 | parseTimesExp(); 142 | throw new Todo(); 143 | } 144 | 145 | // LtExp -> AddSubExp + AddSubExp 146 | // -> AddSubExp - AddSubExp 147 | // -> AddSubExp 148 | private void parseLtExp() { 149 | parseAddSubExp(); 150 | throw new Todo(); 151 | } 152 | 153 | // AndExp -> LtExp < LtExp 154 | // -> LtExp 155 | private void parseAndExp() { 156 | parseLtExp(); 157 | throw new Todo(); 158 | } 159 | 160 | // Exp -> AndExp && AndExp 161 | // -> AndExp 162 | private void parseExp() { 163 | parseAndExp(); 164 | throw new Todo(); 165 | } 166 | 167 | // Statement -> { Statement* } 168 | // -> if ( Exp ) Statement else Statement 169 | // -> while ( Exp ) Statement 170 | // -> System.out.println ( Exp ) ; 171 | // -> id = Exp ; 172 | // -> id [ Exp ]= Exp ; 173 | private void parseStatement() { 174 | // to parse a statement. 175 | throw new Todo(); 176 | } 177 | 178 | // Statements -> Statement Statements 179 | // -> 180 | private void parseStatements() { 181 | throw new Todo(); 182 | } 183 | 184 | // Type -> int [] 185 | // -> boolean 186 | // -> int 187 | // -> id 188 | private void parseType() { 189 | // to parse a type. 190 | throw new Todo(); 191 | } 192 | 193 | // VarDecl -> Type id ; 194 | private void parseVarDecl() throws Exception { 195 | // to parse the "Type" non-terminal in this method, 196 | // instead of writing a fresh one. 197 | parseType(); 198 | eatToken(Token.Kind.ID); 199 | eatToken(Token.Kind.SEMICOLON); 200 | return; 201 | } 202 | 203 | // VarDecls -> VarDecl VarDecls 204 | // -> 205 | private void parseVarDecls() { 206 | throw new util.Todo(); 207 | // return; 208 | } 209 | 210 | // FormalList -> Type id FormalRest* 211 | // -> 212 | // FormalRest -> , Type id 213 | private void parseFormalList() { 214 | throw new Todo(); 215 | } 216 | 217 | // Method -> public Type id ( FormalList ) 218 | // { VarDecl* Statement* return Exp ;} 219 | private void parseMethod() { 220 | // to parse a method. 221 | throw new Todo(); 222 | } 223 | 224 | // MethodDecls -> MethodDecl MethodDecls 225 | // -> 226 | private void parseMethodDecls() { 227 | throw new util.Todo(); 228 | } 229 | 230 | // ClassDecl -> class id { VarDecl* MethodDecl* } 231 | // -> class id extends id { VarDecl* MethodDecl* } 232 | private void parseClassDecl() { 233 | eatToken(Token.Kind.CLASS); 234 | eatToken(Token.Kind.ID); 235 | throw new util.Todo(); 236 | } 237 | 238 | // ClassDecls -> ClassDecl ClassDecls 239 | // -> 240 | private void parseClassDecls() { 241 | while (current.kind.equals(Token.Kind.CLASS)) { 242 | parseClassDecl(); 243 | } 244 | return; 245 | } 246 | 247 | // MainClass -> class id { 248 | // public static void main ( String [] id ) { 249 | // Statement 250 | // } 251 | // } 252 | private void parseMainClass() { 253 | // Lab 1. Exercise 11: Fill in the missing code 254 | // to parse a main class as described by the 255 | // grammar above. 256 | throw new Todo(); 257 | } 258 | 259 | // Program -> MainClass ClassDecl* 260 | private void parseProgram() { 261 | parseMainClass(); 262 | parseClassDecls(); 263 | eatToken(Token.Kind.EOF); 264 | return; 265 | } 266 | 267 | private void initParser() { 268 | try { 269 | this.inputStream = new BufferedInputStream(new FileInputStream(this.inputFileName)); 270 | } catch (Exception e) { 271 | error("unable to open file" + this.inputFileName); 272 | } 273 | 274 | this.lexer = new Lexer(this.inputFileName, this.inputStream); 275 | this.current = lexer.nextToken(); 276 | } 277 | 278 | private void finalizeParser() { 279 | try { 280 | this.inputStream.close(); 281 | } catch (Exception e) { 282 | error("unable to close file"); 283 | } 284 | } 285 | 286 | public Object parse() { 287 | initParser(); 288 | parseProgram(); 289 | finalizeParser(); 290 | return null; 291 | } 292 | } 293 | -------------------------------------------------------------------------------- /src/slp/Compiler.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import slp.Slp.Exp; 4 | import slp.Slp.Exp.Eseq; 5 | import slp.Slp.Exp.Id; 6 | import slp.Slp.Exp.Num; 7 | import slp.Slp.Exp.Op; 8 | import slp.Slp.Stm; 9 | import util.Todo; 10 | 11 | import java.io.FileWriter; 12 | import java.util.HashSet; 13 | import java.util.List; 14 | 15 | // a simple compiler for SLP, to x64. 16 | public class Compiler { 17 | // //////////////////////////////////////// 18 | // whether to keep the generated assembly file. 19 | boolean keepAsm = false; 20 | HashSet ids; 21 | StringBuffer buf; 22 | 23 | private void emit(String s) { 24 | buf.append(s); 25 | } 26 | 27 | private void compileExp(Exp.T exp) { 28 | switch (exp) { 29 | case Id(String x) -> emit("\tmovq\t" + x + ", %rax\n"); 30 | case Num(int n) -> emit("\tmovq\t$" + n + ", %rax\n"); 31 | case Op( 32 | Exp.T left, 33 | String op, 34 | Exp.T right 35 | ) -> { 36 | compileExp(left); 37 | emit("\tpushq\t%rax\n"); 38 | compileExp(right); 39 | emit("\tpopq\t%rdx\n"); 40 | switch (op) { 41 | case "+" -> emit("\taddq\t%rdx, %rax\n"); 42 | case "*" -> emit("\timulq\t%rdx\n"); 43 | default -> throw new Todo(op); 44 | } 45 | } 46 | case Eseq( 47 | Stm.T s, 48 | Exp.T e 49 | ) -> { 50 | compileStm0(s); 51 | compileExp(e); 52 | } 53 | } 54 | } 55 | 56 | // to compile a statement 57 | private void compileStm0(Stm.T s) { 58 | switch (s) { 59 | case Stm.Compound( 60 | Stm.T s1, 61 | Stm.T s2 62 | ) -> { 63 | compileStm0(s1); 64 | compileStm0(s2); 65 | } 66 | case Stm.Assign( 67 | String x, 68 | Exp.T e 69 | ) -> { 70 | ids.add(x); 71 | compileExp(e); 72 | emit("\tmovq\t%rax, " + x + "\n"); 73 | } 74 | case Stm.Print(List exps) -> { 75 | exps.forEach(e -> { 76 | compileExp(e); 77 | emit(""" 78 | movq\t%rax, %rsi 79 | movq\t$slp_format, %rdi 80 | callq\tprintf 81 | """); 82 | }); 83 | emit(""" 84 | movq\t$new_line, %rdi 85 | callq\tprintf 86 | """); 87 | } 88 | } 89 | } 90 | 91 | // //////////////////////////////////////// 92 | public void compileStm(Stm.T prog) throws Exception { 93 | // we always reset these two variables, so that this 94 | // method is re-entrant. 95 | this.ids = new HashSet<>(); 96 | this.buf = new StringBuffer(); 97 | 98 | // do the real work 99 | compileStm0(prog); 100 | 101 | FileWriter fileWriter = new FileWriter("slp_gen.s"); 102 | fileWriter.write( 103 | """ 104 | // Automatically generated by the Tiger compiler, do NOT edit. 105 | // the data section: 106 | .data 107 | slp_format: 108 | .string "%d " 109 | new_line: 110 | .string "\\n" 111 | """); 112 | for (String s : this.ids) { 113 | fileWriter.write(s + ":"); 114 | fileWriter.write("\t.long 0\n"); 115 | } 116 | fileWriter.write( 117 | """ 118 | .text 119 | .globl main 120 | main: 121 | pushq\t%rbp 122 | movq\t%rsp, %rbp 123 | """); 124 | fileWriter.write(buf.toString()); 125 | fileWriter.write("\tleave\n\tret\n\n"); 126 | fileWriter.close(); 127 | 128 | String[] cmdStr = {"gcc", "-no-pie", "slp_gen.s"}; 129 | Process child = Runtime.getRuntime().exec(cmdStr, null, null); 130 | child.waitFor(); 131 | if (!keepAsm) { 132 | String[] cmdStr2 = {"rm", "-rf", "slp_gen.s"}; 133 | Runtime.getRuntime().exec(cmdStr2, null, null); 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/slp/Interpreter.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import slp.Slp.Exp; 4 | import slp.Slp.Stm; 5 | import util.Todo; 6 | 7 | import java.util.HashMap; 8 | 9 | // an interpreter for the SLP language. 10 | public class Interpreter { 11 | // an abstract memory mapping each variable to its value 12 | HashMap memory = new HashMap<>(); 13 | 14 | // /////////////////////////////////////////// 15 | // interpret an expression 16 | private int interpExp(Exp.T exp) { 17 | throw new Todo(exp); 18 | } 19 | 20 | // /////////////////////////////////////////// 21 | // interpret a statement 22 | public void interpStm(Stm.T stm) { 23 | throw new Todo(stm); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/slp/Main.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Main obj = new Main(); 7 | obj.doit(SamplePrograms.sample1); 8 | obj.doit(SamplePrograms.sample2); 9 | } 10 | 11 | public void doit(Slp.Stm.T prog) { 12 | PrettyPrint pp = new PrettyPrint(); 13 | pp.ppStm(prog); 14 | 15 | // maximum argument: 16 | MaxArgument max = new MaxArgument(); 17 | System.out.println(max.maxStm(prog)); 18 | 19 | // interpreter: 20 | Interpreter interp = new Interpreter(); 21 | interp.interpStm(prog); 22 | 23 | // compiler to x64: 24 | Compiler compiler = new Compiler(); 25 | try { 26 | compiler.compileStm(prog); 27 | } catch (Exception e) { 28 | throw new util.Error(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/slp/MaxArgument.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import slp.Slp.Exp; 4 | import slp.Slp.Stm; 5 | import util.Todo; 6 | 7 | public class MaxArgument { 8 | // /////////////////////////////////////////// 9 | // expression 10 | private int maxExp(Exp.T exp) { 11 | throw new Todo(exp); 12 | } 13 | 14 | // /////////////////////////////////////////// 15 | // statement 16 | public int maxStm(Stm.T stm) { 17 | throw new Todo(stm); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/slp/PrettyPrint.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import slp.Slp.Exp; 4 | import slp.Slp.Stm; 5 | 6 | import java.util.List; 7 | 8 | import static java.lang.System.out; 9 | 10 | // this defines a pretty printer for the SLP language. 11 | public class PrettyPrint { 12 | // a few print functions for convenience. 13 | private static void print(String s) { 14 | out.print(s); 15 | } 16 | 17 | private static void println(String s) { 18 | out.println(s); 19 | } 20 | 21 | 22 | // /////////////////////////////////////////// 23 | // print expression 24 | private void ppExp(Exp.T exp) { 25 | switch (exp) { 26 | case Exp.Num(int n) -> print(Integer.valueOf(n).toString()); 27 | case Exp.Id(String x) -> print(x); 28 | case Exp.Op( 29 | Exp.T left, 30 | String bop, 31 | Exp.T right 32 | ) -> { 33 | ppExp(left); 34 | print(bop); 35 | ppExp(right); 36 | } 37 | case Exp.Eseq(Stm.T stm, Exp.T e) -> { 38 | print("("); 39 | ppStm(stm); 40 | print(", "); 41 | ppExp(e); 42 | print(")"); 43 | } 44 | } 45 | } 46 | 47 | // /////////////////////////////////////////// 48 | // print statement 49 | public void ppStm(Stm.T stm) { 50 | switch (stm) { 51 | case Stm.Compound( 52 | Stm.T s1, 53 | Stm.T s2 54 | ) -> { 55 | ppStm(s1); 56 | println(";"); 57 | ppStm(s2); 58 | } 59 | case Stm.Assign( 60 | String x, 61 | Exp.T e 62 | ) -> { 63 | print(x + " = "); 64 | ppExp(e); 65 | } 66 | case Stm.Print(List exps) -> { 67 | System.out.print("print("); 68 | exps.forEach(x -> { 69 | ppExp(x); 70 | print(", "); 71 | } 72 | ); 73 | System.out.print(")"); 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/slp/SamplePrograms.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import slp.Slp.Exp.Eseq; 4 | import slp.Slp.Exp.Id; 5 | import slp.Slp.Exp.Num; 6 | import slp.Slp.Exp.Op; 7 | import slp.Slp.Stm; 8 | import slp.Slp.Stm.Assign; 9 | import slp.Slp.Stm.Compound; 10 | import slp.Slp.Stm.Print; 11 | 12 | import java.util.List; 13 | 14 | // two sample programs. 15 | public class SamplePrograms { 16 | public static Stm.T sample1 = new Compound( 17 | new Assign("a", new Op(new Num(5), "+", new Num(3))), 18 | new Compound( 19 | new Assign("b", new Eseq(new Print(List.of( 20 | new Id("a"), 21 | new Op(new Id("a"), "-", new Num(1)))), 22 | new Op(new Num(10), "*", new Id("a")))), 23 | new Print(List.of(new Id("b"))))); 24 | 25 | // lab 1, exercise 3: 26 | // replace the "null" with your code: 27 | public static Stm.T sample2 = null; 28 | } 29 | -------------------------------------------------------------------------------- /src/slp/Slp.java: -------------------------------------------------------------------------------- 1 | package slp; 2 | 3 | import java.util.List; 4 | 5 | // the abstract syntax trees for the SLP language. 6 | public class Slp { 7 | // //////////////////////////////////////////////// 8 | // expression 9 | public static class Exp { 10 | // the type 11 | public sealed interface T 12 | permits Eseq, Id, Op, Num { 13 | } 14 | 15 | // s, e 16 | public record Eseq(Stm.T stm, 17 | T exp) implements T { 18 | } 19 | 20 | // x 21 | public record Id(String id) implements T { 22 | } 23 | 24 | // e bop e 25 | public record Op(T left, 26 | String op, 27 | T right) implements T { 28 | } 29 | 30 | // n 31 | public record Num(int num) implements T { 32 | } 33 | } 34 | // end of expression 35 | 36 | // /////////////////////////////////////////////// 37 | // statement 38 | public static class Stm { 39 | // the type 40 | public sealed interface T 41 | permits Assign, Compound, Print { 42 | } 43 | 44 | // x := e 45 | public record Assign(String id, 46 | Exp.T exp) implements T { 47 | } 48 | 49 | // s1; s2 50 | public record Compound(T s1, 51 | T s2) implements T { 52 | } 53 | 54 | // print(explist) 55 | public record Print(List exps) implements T { 56 | } 57 | } 58 | // end of statement 59 | } 60 | -------------------------------------------------------------------------------- /src/util/Error.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | public class Error extends AssertionError { 4 | private final String foregroundColor = "\033[31;4m"; 5 | private final String backgroundColor = "\033[0m"; 6 | 7 | public Error() { 8 | super(); 9 | System.out.println("\n" + this.foregroundColor + "Compiler error" + backgroundColor); 10 | } 11 | 12 | public Error(Object obj) { 13 | super(obj); 14 | System.out.println("\n" + this.foregroundColor + "Compiler error" + backgroundColor); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/util/Todo.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | public class Todo extends AssertionError { 4 | private final String foregroundColor = "\033[31;4m"; 5 | private final String backgroundColor = "\033[0m"; 6 | 7 | public Todo() { 8 | super(); 9 | System.out.println("\n" + foregroundColor + "TODO: please add your code here:\n" + backgroundColor); 10 | } 11 | 12 | public Todo(Object o) { 13 | super(o); 14 | System.out.println("\n" + foregroundColor + "TODO: please add your code here:\n" + backgroundColor); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/BinarySearch.java: -------------------------------------------------------------------------------- 1 | class BinarySearch { 2 | public static void main(String[] a) { 3 | System.out.println(new BS().Start(20)); 4 | } 5 | } 6 | // This class contains an array of integers and 7 | // methods to initialize, print and search the array 8 | // using Binary Search 9 | 10 | class BS { 11 | int[] number; 12 | int size; 13 | 14 | // Invoke methods to initialize, print and search 15 | // for elements on the array 16 | public int Start(int sz) { 17 | int aux01; 18 | int aux02; 19 | aux01 = this.Init(sz); 20 | aux02 = this.Print(); 21 | if (this.Search(8)) 22 | System.out.println(1); 23 | else 24 | System.out.println(0); 25 | if (this.Search(19)) 26 | System.out.println(1); 27 | else 28 | System.out.println(0); 29 | if (this.Search(20)) 30 | System.out.println(1); 31 | else 32 | System.out.println(0); 33 | if (this.Search(21)) 34 | System.out.println(1); 35 | else 36 | System.out.println(0); 37 | if (this.Search(37)) 38 | System.out.println(1); 39 | else 40 | System.out.println(0); 41 | if (this.Search(38)) 42 | System.out.println(1); 43 | else 44 | System.out.println(0); 45 | if (this.Search(39)) 46 | System.out.println(1); 47 | else 48 | System.out.println(0); 49 | if (this.Search(50)) 50 | System.out.println(1); 51 | else 52 | System.out.println(0); 53 | 54 | return 999; 55 | } 56 | 57 | // Search for a specific value (num) using 58 | // binary search 59 | public boolean Search(int num) { 60 | boolean bs01; 61 | int right; 62 | int left; 63 | boolean var_cont; 64 | int medium; 65 | int aux01; 66 | int nt; 67 | 68 | aux01 = 0; 69 | bs01 = false; 70 | right = number.length; 71 | right = right - 1; 72 | left = 0; 73 | var_cont = true; 74 | while (var_cont) { 75 | medium = left + right; 76 | medium = this.Div(medium); 77 | aux01 = number[medium]; 78 | if (num < aux01) 79 | right = medium - 1; 80 | else 81 | left = medium + 1; 82 | if (this.Compare(aux01, num)) 83 | var_cont = false; 84 | else 85 | var_cont = true; 86 | if (right < left) 87 | var_cont = false; 88 | else 89 | nt = 0; 90 | } 91 | 92 | if (this.Compare(aux01, num)) 93 | bs01 = true; 94 | else 95 | bs01 = false; 96 | return bs01; 97 | } 98 | 99 | // This method computes and returns the 100 | // integer division of a number (num) by 2 101 | public int Div(int num) { 102 | int count01; 103 | int count02; 104 | int aux03; 105 | 106 | count01 = 0; 107 | count02 = 0; 108 | aux03 = num - 1; 109 | while (count02 < aux03) { 110 | count01 = count01 + 1; 111 | count02 = count02 + 2; 112 | } 113 | return count01; 114 | } 115 | 116 | // This method compares two integers and 117 | // returns true if they are equal and false 118 | // otherwise 119 | public boolean Compare(int num1, int num2) { 120 | boolean retval; 121 | int aux02; 122 | 123 | retval = false; 124 | aux02 = num2 + 1; 125 | if (num1 < num2) 126 | retval = false; 127 | else if (!(num1 < aux02)) 128 | retval = false; 129 | else 130 | retval = true; 131 | return retval; 132 | } 133 | 134 | // Print the integer array 135 | public int Print() { 136 | int j; 137 | 138 | j = 1; 139 | while (j < (size)) { 140 | System.out.println(number[j]); 141 | j = j + 1; 142 | } 143 | System.out.println(99999); 144 | return 0; 145 | } 146 | 147 | // Initialize the integer array 148 | public int Init(int sz) { 149 | int j; 150 | int k; 151 | int aux02; 152 | int aux01; 153 | 154 | size = sz; 155 | number = new int[sz]; 156 | 157 | j = 1; 158 | k = size + 1; 159 | while (j < (size)) { 160 | aux01 = 2 * j; 161 | aux02 = k - 3; 162 | number[j] = aux01 + aux02; 163 | j = j + 1; 164 | k = k - 1; 165 | } 166 | return 0; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /test/BinaryTree.java: -------------------------------------------------------------------------------- 1 | class BinaryTree{ 2 | public static void main(String[] a){ 3 | System.out.println(new BT().Start()); 4 | } 5 | } 6 | 7 | 8 | // This class invokes the methods to create a tree, 9 | // insert, delete and serach for elements on it 10 | class BT { 11 | 12 | public int Start(){ 13 | Tree root ; 14 | boolean ntb ; 15 | int nti ; 16 | 17 | root = new Tree(); 18 | ntb = root.Init(16); 19 | ntb = root.Print(); 20 | System.out.println(100000000); 21 | ntb = root.Insert(8) ; 22 | ntb = root.Print(); 23 | ntb = root.Insert(24) ; 24 | ntb = root.Insert(4) ; 25 | ntb = root.Insert(12) ; 26 | ntb = root.Insert(20) ; 27 | ntb = root.Insert(28) ; 28 | ntb = root.Insert(14) ; 29 | ntb = root.Print(); 30 | System.out.println(root.Search(24)); 31 | System.out.println(root.Search(12)); 32 | System.out.println(root.Search(16)); 33 | System.out.println(root.Search(50)); 34 | System.out.println(root.Search(12)); 35 | ntb = root.Delete(12); 36 | ntb = root.Print(); 37 | System.out.println(root.Search(12)); 38 | 39 | return 0 ; 40 | } 41 | 42 | } 43 | 44 | class Tree{ 45 | Tree left ; 46 | Tree right; 47 | int key ; 48 | boolean has_left ; 49 | boolean has_right ; 50 | Tree my_null ; 51 | 52 | // Initialize a node with a key value and no children 53 | public boolean Init(int v_key){ 54 | key = v_key ; 55 | has_left = false ; 56 | has_right = false ; 57 | return true ; 58 | } 59 | 60 | // Update the right child with rn 61 | public boolean SetRight(Tree rn){ 62 | right = rn ; 63 | return true ; 64 | } 65 | 66 | // Update the left child with ln 67 | public boolean SetLeft(Tree ln){ 68 | left = ln ; 69 | return true ; 70 | } 71 | 72 | public Tree GetRight(){ 73 | return right ; 74 | } 75 | 76 | public Tree GetLeft(){ 77 | return left; 78 | } 79 | 80 | public int GetKey(){ 81 | return key ; 82 | } 83 | 84 | public boolean SetKey(int v_key){ 85 | key = v_key ; 86 | return true ; 87 | } 88 | 89 | public boolean GetHas_Right(){ 90 | return has_right ; 91 | } 92 | 93 | public boolean GetHas_Left(){ 94 | return has_left ; 95 | } 96 | 97 | public boolean SetHas_Left(boolean val){ 98 | has_left = val ; 99 | return true ; 100 | } 101 | 102 | public boolean SetHas_Right(boolean val){ 103 | has_right = val ; 104 | return true ; 105 | } 106 | 107 | // This method compares two integers and 108 | // returns true if they are equal and false 109 | // otherwise 110 | public boolean Compare(int num1 , int num2){ 111 | boolean ntb ; 112 | int nti ; 113 | 114 | ntb = false ; 115 | nti = num2 + 1 ; 116 | if (num1 < num2) ntb = false ; 117 | else if (!(num1 < nti)) ntb = false ; 118 | else ntb = true ; 119 | return ntb ; 120 | } 121 | 122 | 123 | // Insert a new element in the tree 124 | public boolean Insert(int v_key){ 125 | Tree new_node ; 126 | boolean ntb ; 127 | boolean cont ; 128 | int key_aux ; 129 | Tree current_node ; 130 | 131 | new_node = new Tree(); 132 | ntb = new_node.Init(v_key) ; 133 | current_node = this ; 134 | cont = true ; 135 | while (cont){ 136 | key_aux = current_node.GetKey(); 137 | if (v_key < key_aux){ 138 | if (current_node.GetHas_Left()) 139 | current_node = current_node.GetLeft() ; 140 | else { 141 | cont = false ; 142 | ntb = current_node.SetHas_Left(true); 143 | ntb = current_node.SetLeft(new_node); 144 | } 145 | } 146 | else{ 147 | if (current_node.GetHas_Right()) 148 | current_node = current_node.GetRight() ; 149 | else { 150 | cont = false ; 151 | ntb = current_node.SetHas_Right(true); 152 | ntb = current_node.SetRight(new_node); 153 | } 154 | } 155 | } 156 | return true ; 157 | } 158 | 159 | 160 | // Delete an element from the tree 161 | public boolean Delete(int v_key){ 162 | Tree current_node ; 163 | Tree parent_node ; 164 | boolean cont ; 165 | boolean found ; 166 | boolean is_root ; 167 | int key_aux ; 168 | boolean ntb ; 169 | 170 | current_node = this ; 171 | parent_node = this ; 172 | cont = true ; 173 | found = false ; 174 | is_root = true ; 175 | while (cont){ 176 | key_aux = current_node.GetKey(); 177 | if (v_key < key_aux) 178 | if (current_node.GetHas_Left()){ 179 | parent_node = current_node ; 180 | current_node = current_node.GetLeft() ; 181 | } 182 | else cont = false ; 183 | else 184 | if (key_aux < v_key) 185 | if (current_node.GetHas_Right()){ 186 | parent_node = current_node ; 187 | current_node = current_node.GetRight() ; 188 | } 189 | else cont = false ; 190 | else { 191 | if (is_root) 192 | if ((!current_node.GetHas_Right()) && 193 | (!current_node.GetHas_Left()) ) 194 | ntb = true ; 195 | else 196 | ntb = this.Remove(parent_node,current_node); 197 | else ntb = this.Remove(parent_node,current_node); 198 | found = true ; 199 | cont = false ; 200 | } 201 | is_root = false ; 202 | } 203 | return found ; 204 | } 205 | 206 | 207 | // Check if the element to be removed will use the 208 | // righ or left subtree if one exists 209 | public boolean Remove(Tree p_node, Tree c_node){ 210 | boolean ntb ; 211 | int auxkey1 ; 212 | int auxkey2 ; 213 | 214 | if (c_node.GetHas_Left()) 215 | ntb = this.RemoveLeft(p_node,c_node) ; 216 | else 217 | if (c_node.GetHas_Right()) 218 | ntb = this.RemoveRight(p_node,c_node) ; 219 | else { 220 | auxkey1 = c_node.GetKey(); 221 | //auxtree01 = p_node.GetLeft() ; 222 | //auxkey2 = auxtree01.GetKey() ; 223 | auxkey2 = (p_node.GetLeft()).GetKey() ; 224 | if (this.Compare(auxkey1,auxkey2)) { 225 | ntb = p_node.SetLeft(my_null); 226 | ntb = p_node.SetHas_Left(false); 227 | } 228 | else { 229 | ntb = p_node.SetRight(my_null); 230 | ntb = p_node.SetHas_Right(false); 231 | } 232 | } 233 | return true ; 234 | } 235 | 236 | 237 | // Copy the child key to the parent until a leaf is 238 | // found and remove the leaf. This is done with the 239 | // right subtree 240 | public boolean RemoveRight(Tree p_node, Tree c_node){ 241 | boolean ntb ; 242 | 243 | while (c_node.GetHas_Right()){ 244 | //auxtree01 = c_node.GetRight() ; 245 | //auxint02 = auxtree01.GetKey(); 246 | //ntb = c_node.SetKey(auxint02); 247 | ntb = c_node.SetKey((c_node.GetRight()).GetKey()); 248 | p_node = c_node ; 249 | c_node = c_node.GetRight() ; 250 | } 251 | ntb = p_node.SetRight(my_null); 252 | ntb = p_node.SetHas_Right(false); 253 | return true ; 254 | } 255 | 256 | 257 | // Copy the child key to the parent until a leaf is 258 | // found and remove the leaf. This is done with the 259 | // left subtree 260 | public boolean RemoveLeft(Tree p_node, Tree c_node){ 261 | boolean ntb ; 262 | 263 | while (c_node.GetHas_Left()){ 264 | //auxtree01 = c_node.GetLeft() ; 265 | //auxint02 = auxtree01.GetKey(); 266 | //ntb = c_node.SetKey(auxint02); 267 | ntb = c_node.SetKey((c_node.GetLeft()).GetKey()); 268 | p_node = c_node ; 269 | c_node = c_node.GetLeft() ; 270 | } 271 | ntb = p_node.SetLeft(my_null); 272 | ntb = p_node.SetHas_Left(false); 273 | return true ; 274 | } 275 | 276 | // Search for an elemnt in the tree 277 | public int Search(int v_key){ 278 | boolean cont ; 279 | int ifound ; 280 | Tree current_node; 281 | int key_aux ; 282 | 283 | current_node = this ; 284 | cont = true ; 285 | ifound = 0 ; 286 | while (cont){ 287 | key_aux = current_node.GetKey(); 288 | if (v_key < key_aux) 289 | if (current_node.GetHas_Left()) 290 | current_node = current_node.GetLeft() ; 291 | else cont = false ; 292 | else 293 | if (key_aux < v_key) 294 | if (current_node.GetHas_Right()) 295 | current_node = current_node.GetRight() ; 296 | else cont = false ; 297 | else { 298 | ifound = 1 ; 299 | cont = false ; 300 | } 301 | } 302 | return ifound ; 303 | } 304 | 305 | // Invoke the method to really print the tree elements 306 | public boolean Print(){ 307 | Tree current_node; 308 | boolean ntb ; 309 | 310 | current_node = this ; 311 | ntb = this.RecPrint(current_node); 312 | return true ; 313 | } 314 | 315 | // Print the elements of the tree 316 | public boolean RecPrint(Tree node){ 317 | boolean ntb ; 318 | 319 | if (node.GetHas_Left()){ 320 | //auxtree01 = node.GetLeft() ; 321 | //ntb = this.RecPrint(auxtree01); 322 | ntb = this.RecPrint(node.GetLeft()); 323 | } else ntb = true ; 324 | System.out.println(node.GetKey()); 325 | if (node.GetHas_Right()){ 326 | //auxtree01 = node.GetRight() ; 327 | //ntb = this.RecPrint(auxtree01); 328 | ntb = this.RecPrint(node.GetRight()); 329 | } else ntb = true ; 330 | return true ; 331 | } 332 | 333 | } 334 | 335 | -------------------------------------------------------------------------------- /test/BubbleSort.java: -------------------------------------------------------------------------------- 1 | class BubbleSort{ 2 | public static void main(String[] a){ 3 | System.out.println(new BBS().Start(10)); 4 | } 5 | } 6 | 7 | 8 | // This class contains the array of integers and 9 | // methods to initialize, print and sort the array 10 | // using Bublesort 11 | class BBS{ 12 | 13 | int[] number ; 14 | int size ; 15 | 16 | // Invoke the Initialization, Sort and Printing 17 | // Methods 18 | public int Start(int sz){ 19 | int aux01 ; 20 | aux01 = this.Init(sz); 21 | aux01 = this.Print(); 22 | System.out.println(99999); 23 | aux01 = this.Sort(); 24 | aux01 = this.Print(); 25 | return 0 ; 26 | } 27 | 28 | 29 | // Sort array of integers using Bublesort method 30 | public int Sort(){ 31 | int nt ; 32 | int i ; 33 | int aux02 ; 34 | int aux04 ; 35 | int aux05 ; 36 | int aux06 ; 37 | int aux07 ; 38 | int j ; 39 | int t ; 40 | i = size - 1 ; 41 | aux02 = 0 - 1 ; 42 | while (aux02 < i) { 43 | j = 1 ; 44 | //aux03 = i+1 ; 45 | while (j < (i+1)){ 46 | aux07 = j - 1 ; 47 | aux04 = number[aux07] ; 48 | aux05 = number[j] ; 49 | if (aux05 < aux04) { 50 | aux06 = j - 1 ; 51 | t = number[aux06] ; 52 | number[aux06] = number[j] ; 53 | number[j] = t; 54 | } 55 | else nt = 0 ; 56 | j = j + 1 ; 57 | } 58 | i = i - 1 ; 59 | } 60 | return 0 ; 61 | } 62 | 63 | // Printing method 64 | public int Print(){ 65 | int j ; 66 | j = 0 ; 67 | while (j < (size)) { 68 | System.out.println(number[j]); 69 | j = j + 1 ; 70 | } 71 | return 0 ; 72 | } 73 | 74 | // Initialize array of integers 75 | public int Init(int sz){ 76 | size = sz ; 77 | number = new int[sz] ; 78 | 79 | number[0] = 20 ; 80 | number[1] = 7 ; 81 | number[2] = 12 ; 82 | number[3] = 18 ; 83 | number[4] = 2 ; 84 | number[5] = 11 ; 85 | number[6] = 6 ; 86 | number[7] = 9 ; 87 | number[8] = 19 ; 88 | number[9] = 5 ; 89 | 90 | return 0 ; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /test/Factorial.java: -------------------------------------------------------------------------------- 1 | class Factorial { 2 | public static void main(String[] a) { 3 | System.out.println(new Fac().ComputeFac(10)); 4 | } 5 | } 6 | 7 | class Fac { 8 | public int ComputeFac(int num) { 9 | int num_aux; 10 | if (num < 1) 11 | num_aux = 1; 12 | else 13 | num_aux = num * (this.ComputeFac(num - 1)); 14 | return num_aux; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/LinearSearch.java: -------------------------------------------------------------------------------- 1 | class LinearSearch{ 2 | public static void main(String[] a){ 3 | System.out.println(new LS().Start(10)); 4 | } 5 | } 6 | 7 | 8 | // This class contains an array of integers and 9 | // methods to initialize, print and search the array 10 | // using Linear Search 11 | class LS { 12 | int[] number ; 13 | int size ; 14 | 15 | // Invoke methods to initialize, print and search 16 | // for elements on the array 17 | public int Start(int sz){ 18 | int aux01 ; 19 | int aux02 ; 20 | 21 | aux01 = this.Init(sz); 22 | aux02 = this.Print(); 23 | System.out.println(9999); 24 | System.out.println(this.Search(8)); 25 | System.out.println(this.Search(12)) ; 26 | System.out.println(this.Search(17)) ; 27 | System.out.println(this.Search(50)) ; 28 | return 55 ; 29 | } 30 | 31 | // Print array of integers 32 | public int Print(){ 33 | int j ; 34 | 35 | j = 1 ; 36 | while (j < (size)) { 37 | System.out.println(number[j]); 38 | j = j + 1 ; 39 | } 40 | return 0 ; 41 | } 42 | 43 | // Search for a specific value (num) using 44 | // linear search 45 | public int Search(int num){ 46 | int j ; 47 | boolean ls01 ; 48 | int ifound ; 49 | int aux01 ; 50 | int aux02 ; 51 | int nt ; 52 | 53 | j = 1 ; 54 | ls01 = false ; 55 | ifound = 0 ; 56 | 57 | //System.out.println(num); 58 | while (j < (size)) { 59 | aux01 = number[j] ; 60 | aux02 = num + 1 ; 61 | if (aux01 < num) nt = 0 ; 62 | else if (!(aux01 < aux02)) nt = 0 ; 63 | else { 64 | ls01 = true ; 65 | ifound = 1 ; 66 | j = size ; 67 | } 68 | j = j + 1 ; 69 | } 70 | 71 | return ifound ; 72 | } 73 | 74 | 75 | 76 | // initialize array of integers with some 77 | // some sequence 78 | public int Init(int sz){ 79 | int j ; 80 | int k ; 81 | int aux01 ; 82 | int aux02 ; 83 | 84 | size = sz ; 85 | number = new int[sz] ; 86 | 87 | j = 1 ; 88 | k = size + 1 ; 89 | while (j < (size)) { 90 | aux01 = 2 * j ; 91 | aux02 = k - 3 ; 92 | number[j] = aux01 + aux02 ; 93 | j = j + 1 ; 94 | k = k - 1 ; 95 | } 96 | return 0 ; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /test/LinkedList.java: -------------------------------------------------------------------------------- 1 | class LinkedList{ 2 | public static void main(String[] a){ 3 | System.out.println(new LL().Start()); 4 | } 5 | } 6 | 7 | class Element { 8 | int Age ; 9 | int Salary ; 10 | boolean Married ; 11 | 12 | // Initialize some class variables 13 | public boolean Init(int v_Age, int v_Salary, boolean v_Married){ 14 | Age = v_Age ; 15 | Salary = v_Salary ; 16 | Married = v_Married ; 17 | return true ; 18 | } 19 | 20 | public int GetAge(){ 21 | return Age ; 22 | } 23 | 24 | public int GetSalary(){ 25 | return Salary ; 26 | } 27 | 28 | public boolean GetMarried(){ 29 | return Married ; 30 | } 31 | 32 | // This method returns true if the object "other" 33 | // has the same values for age, salary and 34 | public boolean Equal(Element other){ 35 | boolean ret_val ; 36 | int aux01 ; 37 | int aux02 ; 38 | int nt ; 39 | ret_val = true ; 40 | 41 | aux01 = other.GetAge(); 42 | if (!this.Compare(aux01,Age)) ret_val = false ; 43 | else { 44 | aux02 = other.GetSalary(); 45 | if (!this.Compare(aux02,Salary)) ret_val = false ; 46 | else 47 | if (Married) 48 | if (!other.GetMarried()) ret_val = false; 49 | else nt = 0 ; 50 | else 51 | if (other.GetMarried()) ret_val = false; 52 | else nt = 0 ; 53 | } 54 | 55 | return ret_val ; 56 | } 57 | 58 | // This method compares two integers and 59 | // returns true if they are equal and false 60 | // otherwise 61 | public boolean Compare(int num1 , int num2){ 62 | boolean retval ; 63 | int aux02 ; 64 | retval = false ; 65 | aux02 = num2 + 1 ; 66 | if (num1 < num2) retval = false ; 67 | else if (!(num1 < aux02)) retval = false ; 68 | else retval = true ; 69 | return retval ; 70 | } 71 | 72 | } 73 | 74 | class List{ 75 | Element elem ; 76 | List next ; 77 | boolean end ; 78 | 79 | // Initialize the node list as the last node 80 | public boolean Init(){ 81 | end = true ; 82 | return true ; 83 | } 84 | 85 | // Initialize the values of a new node 86 | public boolean InitNew(Element v_elem, List v_next, boolean v_end){ 87 | end = v_end ; 88 | elem = v_elem ; 89 | next = v_next ; 90 | return true ; 91 | } 92 | 93 | // Insert a new node at the beginning of the list 94 | public List Insert(Element new_elem){ 95 | boolean ret_val ; 96 | List aux03 ; 97 | List aux02 ; 98 | aux03 = this ; 99 | aux02 = new List(); 100 | ret_val = aux02.InitNew(new_elem,aux03,false); 101 | return aux02 ; 102 | } 103 | 104 | 105 | // Update the the pointer to the next node 106 | public boolean SetNext(List v_next){ 107 | next = v_next ; 108 | return true ; 109 | } 110 | 111 | // Delete an element e from the list 112 | public List Delete(Element e){ 113 | List my_head ; 114 | boolean ret_val ; 115 | boolean aux05; 116 | List aux01 ; 117 | List prev ; 118 | boolean var_end ; 119 | Element var_elem ; 120 | int aux04 ; 121 | int nt ; 122 | 123 | 124 | my_head = this ; 125 | ret_val = false ; 126 | aux04 = 0 - 1 ; 127 | aux01 = this ; 128 | prev = this ; 129 | var_end = end; 130 | var_elem = elem ; 131 | while ((!var_end) && (!ret_val)){ 132 | if (e.Equal(var_elem)){ 133 | ret_val = true ; 134 | if (aux04 < 0) { 135 | // delete first element 136 | my_head = aux01.GetNext() ; 137 | } 138 | else{ // delete a non first element 139 | System.out.println(0-555); 140 | aux05 = prev.SetNext(aux01.GetNext()); 141 | System.out.println(0-555); 142 | 143 | } 144 | } else nt = 0 ; 145 | if (!ret_val){ 146 | prev = aux01 ; 147 | aux01 = aux01.GetNext() ; 148 | var_end = aux01.GetEnd(); 149 | var_elem = aux01.GetElem(); 150 | aux04 = 1 ; 151 | } else nt = 0 ; 152 | } 153 | return my_head ; 154 | } 155 | 156 | 157 | // Search for an element e on the list 158 | public int Search(Element e){ 159 | int int_ret_val ; 160 | List aux01 ; 161 | Element var_elem ; 162 | boolean var_end ; 163 | int nt ; 164 | 165 | int_ret_val = 0 ; 166 | aux01 = this ; 167 | var_end = end; 168 | var_elem = elem ; 169 | while (!var_end){ 170 | if (e.Equal(var_elem)){ 171 | int_ret_val = 1 ; 172 | } 173 | else nt = 0 ; 174 | aux01 = aux01.GetNext() ; 175 | var_end = aux01.GetEnd(); 176 | var_elem = aux01.GetElem(); 177 | } 178 | return int_ret_val ; 179 | } 180 | 181 | public boolean GetEnd(){ 182 | return end ; 183 | } 184 | 185 | public Element GetElem(){ 186 | return elem ; 187 | } 188 | 189 | public List GetNext(){ 190 | return next ; 191 | } 192 | 193 | 194 | // Print the linked list 195 | public boolean Print(){ 196 | List aux01 ; 197 | boolean var_end ; 198 | Element var_elem ; 199 | 200 | aux01 = this ; 201 | var_end = end ; 202 | var_elem = elem ; 203 | while (!var_end){ 204 | System.out.println(var_elem.GetAge()); 205 | aux01 = aux01.GetNext() ; 206 | var_end = aux01.GetEnd(); 207 | var_elem = aux01.GetElem(); 208 | } 209 | 210 | return true ; 211 | } 212 | } 213 | 214 | 215 | // this class invokes the methods to insert, delete, 216 | // search and print the linked list 217 | class LL{ 218 | 219 | public int Start(){ 220 | 221 | List head ; 222 | List last_elem ; 223 | boolean aux01 ; 224 | Element el01 ; 225 | Element el02 ; 226 | Element el03 ; 227 | 228 | last_elem = new List(); 229 | aux01 = last_elem.Init(); 230 | head = last_elem ; 231 | aux01 = head.Init(); 232 | aux01 = head.Print(); 233 | 234 | // inserting first element 235 | el01 = new Element(); 236 | aux01 = el01.Init(25,37000,false); 237 | head = head.Insert(el01); 238 | aux01 = head.Print(); 239 | System.out.println(10000000); 240 | // inserting second element 241 | el01 = new Element(); 242 | aux01 = el01.Init(39,42000,true); 243 | el02 = el01 ; 244 | head = head.Insert(el01); 245 | aux01 = head.Print(); 246 | System.out.println(10000000); 247 | // inserting third element 248 | el01 = new Element(); 249 | aux01 = el01.Init(22,34000,false); 250 | head = head.Insert(el01); 251 | aux01 = head.Print(); 252 | el03 = new Element(); 253 | aux01 = el03.Init(27,34000,false); 254 | System.out.println(head.Search(el02)); 255 | System.out.println(head.Search(el03)); 256 | System.out.println(10000000); 257 | // inserting fourth element 258 | el01 = new Element(); 259 | aux01 = el01.Init(28,35000,false); 260 | head = head.Insert(el01); 261 | aux01 = head.Print(); 262 | System.out.println(2220000); 263 | 264 | head = head.Delete(el02); 265 | aux01 = head.Print(); 266 | System.out.println(33300000); 267 | 268 | 269 | head = head.Delete(el01); 270 | aux01 = head.Print(); 271 | System.out.println(44440000); 272 | 273 | return 0 ; 274 | 275 | 276 | } 277 | 278 | } 279 | -------------------------------------------------------------------------------- /test/QuickSort.java: -------------------------------------------------------------------------------- 1 | class QuickSort{ 2 | public static void main(String[] a){ 3 | System.out.println(new QS().Start(10)); 4 | } 5 | } 6 | 7 | 8 | // This class contains the array of integers and 9 | // methods to initialize, print and sort the array 10 | // using Quicksort 11 | class QS{ 12 | 13 | int[] number ; 14 | int size ; 15 | 16 | // Invoke the Initialization, Sort and Printing 17 | // Methods 18 | public int Start(int sz){ 19 | int aux01 ; 20 | aux01 = this.Init(sz); 21 | aux01 = this.Print(); 22 | System.out.println(9999); 23 | aux01 = size - 1 ; 24 | aux01 = this.Sort(0,aux01); 25 | aux01 = this.Print(); 26 | return 0 ; 27 | } 28 | 29 | 30 | // Sort array of integers using Quicksort method 31 | public int Sort(int left, int right){ 32 | int v ; 33 | int i ; 34 | int j ; 35 | int nt; 36 | int t ; 37 | boolean cont01; 38 | boolean cont02; 39 | int aux03 ; 40 | t = 0 ; 41 | if (left < right){ 42 | v = number[right] ; 43 | i = left - 1 ; 44 | j = right ; 45 | cont01 = true ; 46 | while (cont01){ 47 | cont02 = true ; 48 | while (cont02){ 49 | i = i + 1 ; 50 | aux03 = number[i] ; 51 | if (!(aux03