├── .classpath ├── .gitattributes ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── README.md ├── Script_1 with LR1 parser generator.zip ├── Script_1 ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Instruction.txt ├── ast_gr_rule.ods ├── bin │ ├── .gitignore │ ├── Interpreter │ │ ├── Interpreter.class │ │ ├── RT_Env.class │ │ ├── RT_Frame.class │ │ ├── RT_Heap.class │ │ ├── RT_Reference.class │ │ ├── RT_Stack.class │ │ ├── RT_Static.class │ │ └── RT_Value.class │ ├── LexAnalyzer │ │ ├── DFA_State.class │ │ ├── LexAnalyzer.class │ │ ├── NFA_State.class │ │ ├── RegexPaser.class │ │ ├── RegexPattern.class │ │ ├── ReservedWord.class │ │ ├── Token$TokenType.class │ │ ├── Token.class │ │ ├── TokenType.class │ │ └── Tokenizer.class │ ├── Parser │ │ ├── AST.class │ │ ├── ASTGenerator.class │ │ ├── ASTRule.class │ │ ├── ASTs │ │ │ ├── AST_Stmt.class │ │ │ ├── AST_StmtList.class │ │ │ ├── Cls_Extd_Lst.class │ │ │ ├── Cls_Impl_Lst.class │ │ │ ├── Expr.class │ │ │ ├── ExprAccs.class │ │ │ ├── ExprAccs_App.class │ │ │ ├── ExprAccs_Arr.class │ │ │ ├── ExprAccs_Fld.class │ │ │ ├── ExprAccs_Pri.class │ │ │ ├── ExprCalc_Add$en_Add.class │ │ │ ├── ExprCalc_Add.class │ │ │ ├── ExprCalc_Bool$en_Bl.class │ │ │ ├── ExprCalc_Bool.class │ │ │ ├── ExprCalc_Cond.class │ │ │ ├── ExprCalc_NewArr.class │ │ │ ├── ExprCalc_NewCls.class │ │ │ ├── ExprCalc_Unary.class │ │ │ ├── ExprPri_Chr.class │ │ │ ├── ExprPri_Num.class │ │ │ ├── ExprPri_Str.class │ │ │ ├── ExprPri_Var.class │ │ │ ├── ExprUnr_Cast.class │ │ │ ├── Expr_Calc.class │ │ │ ├── Expr_Left.class │ │ │ ├── Expr_Lmbd.class │ │ │ ├── FuncApp_ArgLst.class │ │ │ ├── FuncDef_Par.class │ │ │ ├── FuncDef_ParLst.class │ │ │ ├── Gnrc_ArgLst.class │ │ │ ├── Gnrc_Par.class │ │ │ ├── Gnrc_ParLst.class │ │ │ ├── MbrDef.class │ │ │ ├── MbrDef_Fld.class │ │ │ ├── MbrDef_Lst.class │ │ │ ├── MbrDef_Mthd.class │ │ │ ├── NewArr_DimLst.class │ │ │ ├── NewArr_Init.class │ │ │ ├── NewArr_InitLst.class │ │ │ ├── Scp_Info$en_Scp.class │ │ │ ├── Scp_Info.class │ │ │ ├── Scp_InfoLst.class │ │ │ ├── SgStmt_AsgnVar.class │ │ │ ├── SgStmt_CtrFlw$en_Ctrflw.class │ │ │ ├── SgStmt_CtrFlw.class │ │ │ ├── SgStmt_DefVar.class │ │ │ ├── StmtIf_ElsBd.class │ │ │ ├── StmtIf_IfBd.class │ │ │ ├── Stmt_DefCls.class │ │ │ ├── Stmt_DefFunc.class │ │ │ ├── Stmt_DefIntf.class │ │ │ ├── Stmt_If.class │ │ │ ├── Stmt_Sg.class │ │ │ ├── Stmt_Whl.class │ │ │ ├── TypeExp.class │ │ │ ├── TypeExp_Arr.class │ │ │ ├── TypeExp_Bsc.class │ │ │ ├── TypeExp_Func.class │ │ │ ├── TypeExp_Gnrc.class │ │ │ └── TypeExp_Idn.class │ │ ├── CodeGenerator.class │ │ ├── Grammar.class │ │ ├── IR │ │ │ ├── IRCode.class │ │ │ └── IROprator.class │ │ ├── PackageManager.class │ │ ├── ParseState.class │ │ ├── Parser.class │ │ ├── Path.class │ │ ├── Symbol.class │ │ └── TypeSys │ │ │ ├── GenCodeException.class │ │ │ ├── GenSymTblException.class │ │ │ ├── R_Function.class │ │ │ ├── R_Package.class │ │ │ ├── R_Variable.class │ │ │ ├── S_Array.class │ │ │ ├── S_Object.class │ │ │ ├── S_Reference.class │ │ │ ├── S_Value.class │ │ │ ├── T_Alias.class │ │ │ ├── T_Array.class │ │ │ ├── T_BasicType$en_BType.class │ │ │ ├── T_BasicType.class │ │ │ ├── T_Class.class │ │ │ ├── T_Function.class │ │ │ ├── T_Generic.class │ │ │ ├── T_Interface.class │ │ │ ├── T_Type$KType.class │ │ │ ├── T_Type.class │ │ │ ├── TypeCheckException.class │ │ │ └── TypeChecker.class │ ├── ParserGenerator │ │ ├── ActionTable.class │ │ ├── CC.class │ │ ├── Grammar.class │ │ ├── GrammarRule.class │ │ ├── Item.class │ │ ├── ParserGenerator.class │ │ ├── Production.class │ │ └── Symbol.class │ ├── test.class │ └── test1.class ├── grammar.txt ├── grammar_AST.txt ├── main.mf ├── out_cc.txt ├── out_grammar.txt ├── out_lexAnalyzer.txt ├── out_parser.txt ├── out_script_lexer.txt ├── readme.txt ├── script_test1.txt ├── src │ ├── Interpreter │ │ ├── Interpreter.java │ │ ├── RT_Env.java │ │ ├── RT_Frame.java │ │ ├── RT_Heap.java │ │ ├── RT_Reference.java │ │ ├── RT_Stack.java │ │ ├── RT_Static.java │ │ └── RT_Value.java │ ├── LexAnalyzer │ │ ├── LexAnalyzer.java │ │ ├── Token$1.class │ │ ├── Token$TokenType.class │ │ ├── Token.class │ │ ├── Token.java │ │ ├── Tokenizer$1.class │ │ ├── Tokenizer.class │ │ └── Tokenizer.java │ ├── Parser │ │ ├── AST.class │ │ ├── AST.java │ │ ├── ASTGenerator.class │ │ ├── ASTGenerator.java │ │ ├── ASTRule.class │ │ ├── ASTRule.java │ │ ├── ASTs │ │ │ ├── AST_Stmt.class │ │ │ ├── AST_Stmt.java │ │ │ ├── AST_StmtList.class │ │ │ ├── AST_StmtList.java │ │ │ ├── Cls_Extd_Lst.class │ │ │ ├── Cls_Extd_Lst.java │ │ │ ├── Cls_Impl_Lst.class │ │ │ ├── Cls_Impl_Lst.java │ │ │ ├── Expr.class │ │ │ ├── Expr.java │ │ │ ├── ExprAccs.class │ │ │ ├── ExprAccs.java │ │ │ ├── ExprAccs_App.class │ │ │ ├── ExprAccs_App.java │ │ │ ├── ExprAccs_Arr.class │ │ │ ├── ExprAccs_Arr.java │ │ │ ├── ExprAccs_Fld.class │ │ │ ├── ExprAccs_Fld.java │ │ │ ├── ExprAccs_Pri.class │ │ │ ├── ExprAccs_Pri.java │ │ │ ├── ExprCalc_Add$1.class │ │ │ ├── ExprCalc_Add$en_Add.class │ │ │ ├── ExprCalc_Add.class │ │ │ ├── ExprCalc_Add.java │ │ │ ├── ExprCalc_Bool$1.class │ │ │ ├── ExprCalc_Bool$en_Bl.class │ │ │ ├── ExprCalc_Bool.class │ │ │ ├── ExprCalc_Bool.java │ │ │ ├── ExprCalc_Cond.class │ │ │ ├── ExprCalc_Cond.java │ │ │ ├── ExprCalc_NewArr.class │ │ │ ├── ExprCalc_NewArr.java │ │ │ ├── ExprCalc_NewCls.class │ │ │ ├── ExprCalc_NewCls.java │ │ │ ├── ExprCalc_Unary.class │ │ │ ├── ExprCalc_Unary.java │ │ │ ├── ExprPri_Chr.class │ │ │ ├── ExprPri_Chr.java │ │ │ ├── ExprPri_Num.class │ │ │ ├── ExprPri_Num.java │ │ │ ├── ExprPri_Str.class │ │ │ ├── ExprPri_Str.java │ │ │ ├── ExprPri_Var.class │ │ │ ├── ExprPri_Var.java │ │ │ ├── ExprUnr_Cast.class │ │ │ ├── ExprUnr_Cast.java │ │ │ ├── Expr_Calc.class │ │ │ ├── Expr_Calc.java │ │ │ ├── Expr_Left.class │ │ │ ├── Expr_Left.java │ │ │ ├── Expr_Lmbd.class │ │ │ ├── Expr_Lmbd.java │ │ │ ├── FuncApp_ArgLst.class │ │ │ ├── FuncApp_ArgLst.java │ │ │ ├── FuncDef_Par.class │ │ │ ├── FuncDef_Par.java │ │ │ ├── FuncDef_ParLst.class │ │ │ ├── FuncDef_ParLst.java │ │ │ ├── Gnrc_ArgLst.class │ │ │ ├── Gnrc_ArgLst.java │ │ │ ├── Gnrc_Par.class │ │ │ ├── Gnrc_Par.java │ │ │ ├── Gnrc_ParLst.class │ │ │ ├── Gnrc_ParLst.java │ │ │ ├── MbrDef.class │ │ │ ├── MbrDef.java │ │ │ ├── MbrDef_Fld.class │ │ │ ├── MbrDef_Fld.java │ │ │ ├── MbrDef_Lst.class │ │ │ ├── MbrDef_Lst.java │ │ │ ├── MbrDef_Mthd.class │ │ │ ├── MbrDef_Mthd.java │ │ │ ├── NewArr_DimLst.class │ │ │ ├── NewArr_DimLst.java │ │ │ ├── NewArr_Init.class │ │ │ ├── NewArr_Init.java │ │ │ ├── NewArr_InitLst.class │ │ │ ├── NewArr_InitLst.java │ │ │ ├── Scp_Info$en_Scp.class │ │ │ ├── Scp_Info.class │ │ │ ├── Scp_Info.java │ │ │ ├── Scp_InfoLst.class │ │ │ ├── Scp_InfoLst.java │ │ │ ├── SgStmt_AsgnVar.class │ │ │ ├── SgStmt_AsgnVar.java │ │ │ ├── SgStmt_CtrFlw$1.class │ │ │ ├── SgStmt_CtrFlw$en_Ctrflw.class │ │ │ ├── SgStmt_CtrFlw.class │ │ │ ├── SgStmt_CtrFlw.java │ │ │ ├── SgStmt_DefVar.class │ │ │ ├── SgStmt_DefVar.java │ │ │ ├── StmtIf_ElsBd.class │ │ │ ├── StmtIf_ElsBd.java │ │ │ ├── StmtIf_IfBd.class │ │ │ ├── StmtIf_IfBd.java │ │ │ ├── Stmt_DefCls.class │ │ │ ├── Stmt_DefCls.java │ │ │ ├── Stmt_DefFunc.class │ │ │ ├── Stmt_DefFunc.java │ │ │ ├── Stmt_DefIntf.class │ │ │ ├── Stmt_DefIntf.java │ │ │ ├── Stmt_If.class │ │ │ ├── Stmt_If.java │ │ │ ├── Stmt_Sg.class │ │ │ ├── Stmt_Sg.java │ │ │ ├── Stmt_Whl.class │ │ │ ├── Stmt_Whl.java │ │ │ ├── TypeExp.class │ │ │ ├── TypeExp.java │ │ │ ├── TypeExp_Arr.class │ │ │ ├── TypeExp_Arr.java │ │ │ ├── TypeExp_Bsc.class │ │ │ ├── TypeExp_Bsc.java │ │ │ ├── TypeExp_Func.class │ │ │ ├── TypeExp_Func.java │ │ │ ├── TypeExp_Gnrc.class │ │ │ ├── TypeExp_Gnrc.java │ │ │ ├── TypeExp_Idn.class │ │ │ └── TypeExp_Idn.java │ │ ├── CodeGenerator$1.class │ │ ├── CodeGenerator.class │ │ ├── CodeGenerator.java │ │ ├── Grammar.class │ │ ├── Grammar.java │ │ ├── IR │ │ │ ├── IRCode.class │ │ │ ├── IRCode.java │ │ │ └── IROprator.java │ │ ├── PackageManager.class │ │ ├── PackageManager.java │ │ ├── ParseState.class │ │ ├── ParseState.java │ │ ├── Parser.class │ │ ├── Parser.java │ │ ├── Path.class │ │ ├── Path.java │ │ ├── Symbol.class │ │ ├── Symbol.java │ │ └── TypeSys │ │ │ ├── GenCodeException.class │ │ │ ├── GenCodeException.java │ │ │ ├── GenSymTblException.class │ │ │ ├── GenSymTblException.java │ │ │ ├── R_Function.class │ │ │ ├── R_Function.java │ │ │ ├── R_Package.java │ │ │ ├── R_Variable.class │ │ │ ├── R_Variable.java │ │ │ ├── S_Array.java │ │ │ ├── S_Object.java │ │ │ ├── S_Reference.java │ │ │ ├── S_Value.java │ │ │ ├── T_Alias.java │ │ │ ├── T_Array.class │ │ │ ├── T_Array.java │ │ │ ├── T_BasicType$1.class │ │ │ ├── T_BasicType$en_BType.class │ │ │ ├── T_BasicType.class │ │ │ ├── T_BasicType.java │ │ │ ├── T_Class.class │ │ │ ├── T_Class.java │ │ │ ├── T_Function.class │ │ │ ├── T_Function.java │ │ │ ├── T_Generic.class │ │ │ ├── T_Generic.java │ │ │ ├── T_Interface.class │ │ │ ├── T_Interface.java │ │ │ ├── T_Type$KType.class │ │ │ ├── T_Type.class │ │ │ ├── T_Type.java │ │ │ ├── TypeCheckException.class │ │ │ ├── TypeCheckException.java │ │ │ ├── TypeChecker.class │ │ │ └── TypeChecker.java │ ├── ParserGenerator │ │ └── ParserGenerator.java │ ├── test.class │ ├── test.java │ └── test1.java ├── testcode │ ├── bin │ │ ├── script1.yfc │ │ └── yftest │ │ │ └── test │ │ │ └── script1.yfc │ ├── script1.yfl │ └── yftest │ │ └── test │ │ └── script1.yfl ├── tokens.txt └── yflang.jar └── SimpleCalc_1 with LL manual parser.zip /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Script_1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #stop next generation plan temporarily. 2 | I found another instersting thing: Javascript and TypeScript. 3 | I will take a period of time to research TypeScript, and to see what can i do. 4 | 5 | #Here is YF virtual machine 6 | https://www.github.com/nklofy/YFVM 7 | 8 | YF virtual machine (YFVM) is an abstract computing machine that enables IR codes of YF programming language(YFlang) to run on it. 9 |
10 | #YF Programming Language 11 | 12 | 13 | 1. toolkit: Parser generator and tokenizer generator. 14 | - GLR parser generator. Write grammers in the file "grammer.txt", then run Parsergenerator.java. shift-reduce table will be generated in "out_grammar.txt". Before run AST_Generator, define actions in "grammar_AST.txt" which should be aligned with actions in ASTGenerator.java. 15 | - Tokenizer generator. Write all words and regex expressions in the file "tokens.txt", then run LexAnalyzer.java. APIs in tokenizer.java can be used for get tokens in files. 16 | 17 | 18 | 2. script and type definition. script part and type definition can be in arbitrary order in the source file. In script part, var define and assignment statments, control structures, fucntion definition and calling are almost the same with convensional programming languages. In type definition part, class and interface are defined, as well as their field and method members. 19 | 20 | 21 | 3. package management. A source file is end with ".yfl" postfix, and IR codes is end with ".yfc" postfix. Usually yfl files are in "src" file folder (or not), yfc files are in "bin" file folder which is generated automatically(no matter if yfl files in src folder). Current package and imported packages should be in the front of yfl file. Classes and interfaces in other packages can be used when imported, but variables and function in script part in other packages can not be imported. There is a demo in "testcode" dir, with "script1.yfl" as sorce file and "bin/script1.yfc" as object file. Run src/test.java to watch the compilation process. 22 | 23 | 24 | 4. Staticly type checking. basic types, expression, function, class, interface. 25 | 26 | 5. Dynamically type checking. type parameters of generic type and function, higher-order function. 27 | 28 | 6. lambda expression. higher-order function. 29 | 30 | 7. IR code. Three address linear code. 31 | 32 | ###Examples: 33 | ``` 34 | //Church number 35 | function zero=(function f, int s)->{return f(s);}; 36 | function inc_1=(function m, function f, int s)->{return (function f, int s)->{return f(m (f, s));};}; 37 | function add_2=(function m, function n, function f, int s)->{return (function f, int s)->{return m (f (n (f (s))));};}; 38 | function f; int s; 39 | //1=0' 40 | function n_1=inc_1(zero, f, s); 41 | //2=1+1=0'+0' 42 | function n_2=add_2(n_1, n_1, f, s); 43 | 44 | //class, generic, hierarchy 45 | class A{ 46 | T1 a; 47 | T1 f(T1 a){this.a=a; return new T1();} 48 | } 49 | 50 | class B extends A{ 51 | T1 b; 52 | T2 g(){return new T2();} 53 | } 54 | 55 | A b=new B(); 56 | int i=b.f(1); 57 | bool j=((B)b).g(); 58 | 59 | ``` 60 | 61 | ##older version: 62 | Script_1 with LR1 parser generator.zip 63 | 64 | 65 | 66 | Compiler and interpreter, with tools of LR(1) parser generator and tokenizer generator. These tools are used for compiling a script program language "YFLang". The IR is just represented by AST. Lex and syntax specifications are defined in some txt files, as well as the parsing rules. The script languege is like a subset of C languese, including if...else... expression, while(...){...} expression, variable defining and assignment, function defining and calling, recursive function, for example: 67 | ``` 68 | int fact(int a){if(a<1) return 1; return a*fact(a-1);} 69 | int fib(int a){if(a<2) return 1; return fib(a-1)+fib(a-2);} 70 | -------------------------------------------------------------------------------- /Script_1 with LR1 parser generator.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1 with LR1 parser generator.zip -------------------------------------------------------------------------------- /Script_1/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Script_1/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Script_1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Script_1/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Script_1/Instruction.txt: -------------------------------------------------------------------------------- 1 | 2 | Please import the project to eclipse javaSE IDE. As I tested, it can run under linux and windows7. 3 | 4 | -------------------------------------------------- 5 | 6 | how to use lexanalyzer? 7 | 8 | 1, input file is "tokens.txt", and output file is "out_lexAnalyzer". 9 | 10 | 2, run lexAnalyzer.java. Its main method read input file and write output file. while running, it will print some information on console screan. 11 | 12 | 3, in input file, tokens is defined in 3 types. firstly, under "//tokens" line, regexs are defined; Secondly, under "//reserved" line, reserved words are difined; at last, under "//operator", opretors are defined. 13 | 14 | 4, in output file, under "//transfer table" line, a transfer table is defined, describing a transfer table of a state auto-machine. each state is in two rows, in which the character and the tranfering state on the character is defined. then under "//regex" line, the final state and its equivalent regex is defined. At last is "//res", "//opt" line and the final states of reserved words and oprators are defined, respectively. 15 | 16 | 5, run tokenizer's main method. It will read "script_test1.txt" and write "out_scipt_lexer.txt". It run a auto-machine defined in "out_lexAnalyzer.txt". Its "getToken" method can get each token sequentially from the file "script_test1.txt" and mark them according to its types and names. 17 | 18 | ---------------------------------------------------------- 19 | 20 | how to use parserGenerator? 21 | 1, input file is "grammar.txt", and output file is "out_cc.txt" and "out_grammar.txt". 22 | 23 | 2, run ParserGenerator.java. Its main method read input file and write output file. while running, it will print some information on console screan. 24 | 25 | 3, In input file, symbols, tokens, grammar generation rules are defined. Under "//symbols" line, there are the symbols, that are non-terminal symbols in the syntax productions. Under "//tokens" are tokens which are terminal symbols. Then under "//grammars" line, the syxtax production rules are defined. After head symbol and a "->" mark, there are production rules of the head, which are separated by "|" marks. 26 | 27 | 4, In output file, action table, goto table and grammar rules is shown. action table describe action of shifting and reducing when parser read the lookahead token, and the rule which should be used for symbols production. State 0 is the start, and when reduced to 0 state and "eof" is lookahead token, the state is end and accept. 28 | 29 | ----------------------------------------------------------- 30 | ----------------------------------------------------------- -------------------------------------------------------------------------------- /Script_1/ast_gr_rule.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/ast_gr_rule.ods -------------------------------------------------------------------------------- /Script_1/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /Parser/ 2 | -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/Interpreter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/Interpreter.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Env.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Env.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Frame.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Frame.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Heap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Heap.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Reference.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Reference.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Stack.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Static.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Static.class -------------------------------------------------------------------------------- /Script_1/bin/Interpreter/RT_Value.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Interpreter/RT_Value.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/DFA_State.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/DFA_State.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/LexAnalyzer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/LexAnalyzer.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/NFA_State.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/NFA_State.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/RegexPaser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/RegexPaser.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/RegexPattern.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/RegexPattern.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/ReservedWord.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/ReservedWord.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/Token$TokenType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/Token$TokenType.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/Token.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/Token.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/TokenType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/TokenType.class -------------------------------------------------------------------------------- /Script_1/bin/LexAnalyzer/Tokenizer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/LexAnalyzer/Tokenizer.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/AST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/AST.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTGenerator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTGenerator.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTRule.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTRule.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/AST_Stmt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/AST_Stmt.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/AST_StmtList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/AST_StmtList.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Cls_Extd_Lst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Cls_Extd_Lst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Cls_Impl_Lst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Cls_Impl_Lst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Expr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Expr.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprAccs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprAccs.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprAccs_App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprAccs_App.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprAccs_Arr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprAccs_Arr.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprAccs_Fld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprAccs_Fld.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprAccs_Pri.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprAccs_Pri.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Add$en_Add.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Add$en_Add.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Add.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Add.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Bool$en_Bl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Bool$en_Bl.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Bool.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Bool.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Cond.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Cond.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_NewArr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_NewArr.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_NewCls.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_NewCls.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprCalc_Unary.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprCalc_Unary.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprPri_Chr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprPri_Chr.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprPri_Num.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprPri_Num.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprPri_Str.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprPri_Str.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprPri_Var.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprPri_Var.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/ExprUnr_Cast.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/ExprUnr_Cast.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Expr_Calc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Expr_Calc.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Expr_Left.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Expr_Left.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Expr_Lmbd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Expr_Lmbd.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/FuncApp_ArgLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/FuncApp_ArgLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/FuncDef_Par.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/FuncDef_Par.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/FuncDef_ParLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/FuncDef_ParLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Gnrc_ArgLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Gnrc_ArgLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Gnrc_Par.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Gnrc_Par.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Gnrc_ParLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Gnrc_ParLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/MbrDef.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/MbrDef.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/MbrDef_Fld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/MbrDef_Fld.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/MbrDef_Lst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/MbrDef_Lst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/MbrDef_Mthd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/MbrDef_Mthd.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/NewArr_DimLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/NewArr_DimLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/NewArr_Init.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/NewArr_Init.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/NewArr_InitLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/NewArr_InitLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Scp_Info$en_Scp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Scp_Info$en_Scp.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Scp_Info.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Scp_Info.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Scp_InfoLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Scp_InfoLst.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/SgStmt_AsgnVar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/SgStmt_AsgnVar.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/SgStmt_CtrFlw$en_Ctrflw.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/SgStmt_CtrFlw$en_Ctrflw.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/SgStmt_CtrFlw.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/SgStmt_CtrFlw.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/SgStmt_DefVar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/SgStmt_DefVar.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/StmtIf_ElsBd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/StmtIf_ElsBd.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/StmtIf_IfBd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/StmtIf_IfBd.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_DefCls.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_DefCls.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_DefFunc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_DefFunc.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_DefIntf.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_DefIntf.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_If.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_If.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_Sg.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_Sg.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/Stmt_Whl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/Stmt_Whl.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp_Arr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp_Arr.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp_Bsc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp_Bsc.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp_Func.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp_Func.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp_Gnrc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp_Gnrc.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ASTs/TypeExp_Idn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ASTs/TypeExp_Idn.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/CodeGenerator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/CodeGenerator.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/Grammar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/Grammar.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/IR/IRCode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/IR/IRCode.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/IR/IROprator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/IR/IROprator.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/PackageManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/PackageManager.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/ParseState.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/ParseState.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/Parser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/Parser.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/Path.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/Path.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/Symbol.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/Symbol.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/GenCodeException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/GenCodeException.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/GenSymTblException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/GenSymTblException.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/R_Function.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/R_Function.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/R_Package.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/R_Package.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/R_Variable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/R_Variable.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/S_Array.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/S_Array.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/S_Object.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/S_Object.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/S_Reference.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/S_Reference.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/S_Value.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/S_Value.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Alias.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Alias.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Array.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Array.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_BasicType$en_BType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_BasicType$en_BType.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_BasicType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_BasicType.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Class.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Class.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Function.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Function.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Generic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Generic.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Interface.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Interface.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Type$KType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Type$KType.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/T_Type.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/T_Type.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/TypeCheckException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/TypeCheckException.class -------------------------------------------------------------------------------- /Script_1/bin/Parser/TypeSys/TypeChecker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/Parser/TypeSys/TypeChecker.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/ActionTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/ActionTable.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/CC.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/CC.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/Grammar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/Grammar.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/GrammarRule.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/GrammarRule.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/Item.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/Item.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/ParserGenerator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/ParserGenerator.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/Production.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/Production.class -------------------------------------------------------------------------------- /Script_1/bin/ParserGenerator/Symbol.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/ParserGenerator/Symbol.class -------------------------------------------------------------------------------- /Script_1/bin/test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/test.class -------------------------------------------------------------------------------- /Script_1/bin/test1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/bin/test1.class -------------------------------------------------------------------------------- /Script_1/main.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: test 3 | 4 | -------------------------------------------------------------------------------- /Script_1/out_parser.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/out_parser.txt -------------------------------------------------------------------------------- /Script_1/readme.txt: -------------------------------------------------------------------------------- 1 | every one can use the source code freely. 2 | please see instruction. 3 | -------------------------------------------------- 4 | 5 | lex analyzer/ auto-machine generator 6 | 7 | tokenizer 8 | 9 | LR(1) parser generator 10 | 11 | LR(1) parser 12 | 13 | Interpreter 14 | -------------------------------------------------------------------------------- /Script_1/script_test1.txt: -------------------------------------------------------------------------------- 1 | /* 2 | int fact(int a){ 3 | if(a==1) return 1; 4 | else 5 | return a*fact(a-1); 6 | } 7 | int fib(int a){ 8 | if(a<3) return 1; 9 | else 10 | return fib(a-1)+fib(a-2); 11 | } 12 | int a=1; 13 | int b=fib(a); 14 | 15 | int[][] as=new int[10][10]; 16 | as[0][0]=6; 17 | int b=as[1][1]; 18 | 19 | class A{ 20 | T1 a; 21 | T1 f(T2 b,T3 c){return this.a;} 22 | } 23 | 24 | class B extends A{ 25 | T1 b; 26 | T1 f(T2 b,T3 c){return this.b;} 27 | } 28 | 29 | A ga=new B(); 30 | bool t=ga.f(1,2); 31 | 32 | function f = ( int a ) -> { return ()->{return 1;}; }; 33 | f(1); 34 | */ 35 | 36 | class Functor { 37 | T1 t; 38 | Functor fmap (function f, Functor wa) { 39 | T2 b=f(wa.t); 40 | Functor fb=new Functor(); 41 | fb.t=b; 42 | return fb; 43 | } 44 | } 45 | 46 | Functor a=new Functor(); 47 | a.t=1; 48 | Functor b=new Functor(); 49 | function f=(int i)->{if(i>0)return true; else return false;}; 50 | b=a.fmap( f , a); 51 | 52 | 53 | class Monad extends Functor{ 54 | Monad rt(T1 t){ 55 | Monad ma=new Monad(); 56 | ma.t=t; 57 | return ma; 58 | } 59 | Monad lk(Monad ma, function f){ 60 | Monad mb=f(ma.t); 61 | return mb; 62 | } 63 | } -------------------------------------------------------------------------------- /Script_1/src/Interpreter/Interpreter.java: -------------------------------------------------------------------------------- 1 | package Interpreter; 2 | 3 | import Parser.Parser; 4 | 5 | public class Interpreter { 6 | public static void main(String[] args) { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Env.java: -------------------------------------------------------------------------------- 1 | //types, functions store in environment 2 | package Interpreter; 3 | import java.util.*; 4 | 5 | import Parser.ASTs.*; 6 | 7 | public class RT_Env { 8 | } 9 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Frame.java: -------------------------------------------------------------------------------- 1 | //frame for function call, args, temp vars 2 | package Interpreter; 3 | 4 | import java.util.*; 5 | 6 | import Parser.ASTs.*; 7 | 8 | public class RT_Frame { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Heap.java: -------------------------------------------------------------------------------- 1 | package Interpreter; 2 | 3 | public class RT_Heap { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Reference.java: -------------------------------------------------------------------------------- 1 | package Interpreter; 2 | 3 | public class RT_Reference { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Stack.java: -------------------------------------------------------------------------------- 1 | package Interpreter; 2 | 3 | public class RT_Stack { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Static.java: -------------------------------------------------------------------------------- 1 | //static objs and functions 2 | package Interpreter; 3 | 4 | import java.util.*; 5 | 6 | public class RT_Static { 7 | } 8 | -------------------------------------------------------------------------------- /Script_1/src/Interpreter/RT_Value.java: -------------------------------------------------------------------------------- 1 | package Interpreter; 2 | 3 | public class RT_Value { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Token$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/LexAnalyzer/Token$1.class -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Token$TokenType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/LexAnalyzer/Token$TokenType.class -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Token.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/LexAnalyzer/Token.class -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Token.java: -------------------------------------------------------------------------------- 1 | package LexAnalyzer; 2 | //4 types of token 3 | public class Token { 4 | static Token create(String pattern, String buffer){ 5 | Token token=new Token(); 6 | switch(pattern){ 7 | case "int": 8 | token.createInt(buffer); 9 | break; 10 | case "double": 11 | token.createDouble(buffer); 12 | break; 13 | case "idn": 14 | token.createIdn(buffer); 15 | break; 16 | case "res": 17 | token.createRes(buffer); 18 | break; 19 | case "opt": 20 | token.createOpt(buffer); 21 | break; 22 | case "note": 23 | token.createNote(buffer); 24 | break; 25 | case "string": 26 | token.createStr(buffer); 27 | break; 28 | case "char": 29 | token.createChr(buffer); 30 | break; 31 | default: 32 | return null; 33 | } 34 | return token; 35 | } 36 | int line_in; 37 | String num_value; 38 | String res_name; 39 | String idn_name; 40 | String opt_name; 41 | String note_value; 42 | String str_value; 43 | String chr_value; 44 | TokenType type; 45 | public int getLine(){ 46 | return line_in; 47 | } 48 | public String getType(){ 49 | switch(type){ 50 | case t_int: 51 | return "int"; 52 | case t_double: 53 | return "double"; 54 | case t_idn: 55 | return "var"; 56 | case t_res: 57 | return "res"; 58 | case t_opt: 59 | return "opt"; 60 | case t_note: 61 | return "note"; 62 | case t_str: 63 | return "string"; 64 | case t_chr: 65 | return "char"; 66 | } 67 | return null; 68 | } 69 | public String getResName(){ 70 | return res_name; 71 | } 72 | public String getOptName(){ 73 | return opt_name; 74 | } 75 | public String getIdnName(){ 76 | return idn_name; 77 | } 78 | public String getNumValue(){ 79 | return num_value; 80 | } 81 | public String getStrValue(){ 82 | return str_value; 83 | } 84 | public String getChrValue(){ 85 | return chr_value; 86 | } 87 | void createInt(String buffer){ 88 | this.type=TokenType.t_int; 89 | this.num_value=buffer; 90 | } 91 | void createDouble(String buffer){ 92 | this.type=TokenType.t_double; 93 | this.num_value=buffer; 94 | } 95 | void createIdn(String buffer){ 96 | this.type=TokenType.t_idn; 97 | this.idn_name=buffer; 98 | } 99 | void createRes(String buffer){ 100 | this.type=TokenType.t_res; 101 | this.res_name=buffer; 102 | } 103 | void createOpt(String buffer){ 104 | this.type=TokenType.t_opt; 105 | this.opt_name=buffer; 106 | } 107 | void createNote(String buffer){ 108 | this.type=TokenType.t_note; 109 | this.note_value=buffer; 110 | } 111 | void createStr(String buffer){ 112 | this.type=TokenType.t_str; 113 | this.str_value=buffer; 114 | } 115 | void createChr(String buffer){ 116 | this.type=TokenType.t_chr; 117 | this.chr_value=buffer; 118 | } 119 | enum TokenType{ 120 | t_int,t_double,t_idn,t_res,t_opt,t_note,t_str,t_chr 121 | }; 122 | } 123 | 124 | -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Tokenizer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/LexAnalyzer/Tokenizer$1.class -------------------------------------------------------------------------------- /Script_1/src/LexAnalyzer/Tokenizer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/LexAnalyzer/Tokenizer.class -------------------------------------------------------------------------------- /Script_1/src/Parser/AST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/AST.class -------------------------------------------------------------------------------- /Script_1/src/Parser/AST.java: -------------------------------------------------------------------------------- 1 | package Parser; 2 | import java.util.*; 3 | 4 | import Interpreter.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class AST { 8 | private int line; 9 | private String ast_type; 10 | private AST ast_deMrg; //de-merge, choose correct one 11 | HashSet merged_asts; 12 | private boolean isMerged=false; 13 | HashMap var_table=new HashMap(); 14 | HashMap type_table=new HashMap(); 15 | HashMap func_table = new HashMap(); 16 | protected int scope=1; 17 | public int getScope() { 18 | return scope; 19 | } 20 | public void setScope(int scope) { 21 | this.scope = scope; 22 | } 23 | public String getASTType() { 24 | return ast_type; 25 | } 26 | public void setASTType(String type) { 27 | String[] ss=type.split("\\."); 28 | this.ast_type = ss[ss.length-1]; 29 | } 30 | public boolean genCode(CodeGenerator codegen) throws GenCodeException{ 31 | return true; 32 | } 33 | public boolean genSymTb(CodeGenerator codegen) throws GenSymTblException{ 34 | return true; 35 | } 36 | public boolean checkType(CodeGenerator codegen) throws TypeCheckException{ 37 | return true; 38 | } 39 | HashMap getVarTb() { 40 | return var_table; 41 | } 42 | void setVarTb(HashMap t) { 43 | this.var_table=t; 44 | } 45 | void putVarTb(String name, R_Variable r) { 46 | if(this.var_table != null){ 47 | this.var_table.put(name, r); 48 | }else{ 49 | this.var_table=new HashMap(); 50 | this.var_table.put(name, r); 51 | } 52 | } 53 | HashMap getTypeTb() { 54 | return type_table; 55 | } 56 | void setTypeTb(HashMap t) { 57 | this.type_table=t; 58 | } 59 | void putTypeTb(String name, T_Type r) { 60 | if(this.type_table != null){ 61 | this.type_table.put(name, r); 62 | }else{ 63 | this.type_table=new HashMap(); 64 | this.type_table.put(name, r); 65 | } 66 | } 67 | HashMap getFuncTb() { 68 | return func_table; 69 | } 70 | void setFuncTb(HashMap t) { 71 | this.func_table=t; 72 | } 73 | void putFuncTb(String name, R_Function r) { 74 | if(this.func_table != null){ 75 | if(this.func_table.containsKey(name)){ 76 | if(this.func_table.get(name).isMulti()){ 77 | this.func_table.get(name).addFuncR(r); 78 | }else{ 79 | this.func_table.get(name).setMulti(); 80 | this.func_table.get(name).addFuncR(r); 81 | } 82 | }else{ 83 | this.func_table.put(name, r); 84 | } 85 | }else{ 86 | this.func_table=new HashMap(); 87 | this.func_table.put(name, r); 88 | } 89 | } 90 | public boolean isMerged() { 91 | return isMerged; 92 | } 93 | public void setMerged() { 94 | this.isMerged = true; 95 | } 96 | public AST getDeMrg(CodeGenerator codegen) { 97 | //de-merge and return 98 | return ast_deMrg; 99 | } 100 | public HashSet getMergedAsts() { 101 | return merged_asts; 102 | } 103 | public void setMergedAsts(HashSet merged_asts) { 104 | this.merged_asts = merged_asts; 105 | } 106 | public int getLine() { 107 | return line; 108 | } 109 | public void setLine(int line) { 110 | this.line = line; 111 | } 112 | } -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTGenerator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTGenerator.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTRule.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTRule.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTRule.java: -------------------------------------------------------------------------------- 1 | package Parser; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ASTRule { 6 | ASTRule(){} 7 | ASTRule(String method){ 8 | this.method=method; 9 | } 10 | String method; 11 | int symbol_count; 12 | ArrayList parameters=new ArrayList(); 13 | } -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/AST_Stmt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/AST_Stmt.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/AST_Stmt.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Interpreter.*; 4 | import Parser.*; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class AST_Stmt extends AST { 10 | Stmt_DefCls stmt_cls; 11 | Stmt_DefIntf stmt_intf; 12 | Stmt_DefFunc stmt_func; 13 | Stmt_If stmt_if; 14 | Stmt_Whl stmt_whl; 15 | Stmt_Sg stmt_sg; 16 | 17 | public boolean setStmt(AST ast){ 18 | switch(this.getASTType()){ 19 | case "Stmt_DefCls": 20 | this.stmt_cls=(Stmt_DefCls)ast; 21 | break; 22 | case "Stmt_DefIntf": 23 | this.stmt_intf=(Stmt_DefIntf)ast; 24 | break; 25 | case "Stmt_DefFunc": 26 | this.stmt_func=(Stmt_DefFunc)ast; 27 | break; 28 | case "Stmt_If": 29 | this.stmt_if=(Stmt_If)ast; 30 | break; 31 | case "Stmt_Whl": 32 | this.stmt_whl=(Stmt_Whl)ast; 33 | break; 34 | case "Stmt_Sg": 35 | this.stmt_sg=(Stmt_Sg)ast; 36 | break; 37 | default: 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 44 | switch(this.getASTType()){ 45 | case "Stmt_DefCls": 46 | this.stmt_cls.genCode(codegen); 47 | break; 48 | case "Stmt_DefIntf": 49 | this.stmt_intf.genCode(codegen); 50 | break; 51 | case "Stmt_DefFunc": 52 | this.stmt_func.genCode(codegen); 53 | break; 54 | case "Stmt_If": 55 | this.stmt_if.genCode(codegen); 56 | break; 57 | case "Stmt_Whl": 58 | this.stmt_whl.genCode(codegen); 59 | break; 60 | case "Stmt_Sg": 61 | this.stmt_sg.genCode(codegen); 62 | break; 63 | default: 64 | return false; 65 | } 66 | return true; 67 | } 68 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 69 | switch(this.getASTType()){ 70 | case "Stmt_DefCls": 71 | return this.stmt_cls.genSymTb(codegen); 72 | case "Stmt_DefIntf": 73 | return this.stmt_intf.genSymTb(codegen); 74 | case "Stmt_DefFunc": 75 | return this.stmt_func.genSymTb(codegen); 76 | case "Stmt_If": 77 | return this.stmt_if.genSymTb(codegen); 78 | case "Stmt_Whl": 79 | return this.stmt_whl.genSymTb(codegen); 80 | case "Stmt_Sg": 81 | return this.stmt_sg.genSymTb(codegen); 82 | default: 83 | return false; 84 | } 85 | } 86 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 87 | boolean rst=true; 88 | switch(this.getASTType()){ 89 | case "Stmt_DefCls": 90 | return this.stmt_cls.checkType(codegen); 91 | case "Stmt_DefIntf": 92 | return this.stmt_intf.checkType(codegen); 93 | case "Stmt_DefFunc": 94 | return this.stmt_func.checkType(codegen); 95 | case "Stmt_If": 96 | return this.stmt_if.checkType(codegen); 97 | case "Stmt_Whl": 98 | return this.stmt_whl.checkType(codegen); 99 | case "Stmt_Sg": 100 | return this.stmt_sg.checkType(codegen); 101 | default: 102 | return false; 103 | } 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/AST_StmtList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/AST_StmtList.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/AST_StmtList.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Interpreter.*; 6 | import Parser.*; 7 | import Parser.TypeSys.GenCodeException; 8 | import Parser.TypeSys.GenSymTblException; 9 | import Parser.TypeSys.TypeCheckException; 10 | 11 | public class AST_StmtList extends AST { 12 | private ArrayList stmt_list=new ArrayList(); 13 | 14 | public void addStmt(AST stmt){ 15 | this.stmt_list.add(stmt); 16 | } 17 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 18 | codegen.pushBlock4Sym(this); 19 | for(AST stmt:this.stmt_list){ 20 | stmt.genCode(codegen); 21 | } 22 | codegen.popBlock4Sym(); 23 | return true; 24 | } 25 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 26 | codegen.pushBlock4Sym(this); 27 | this.setScope(codegen.getScope()); 28 | for(int i=0;i exts; 11 | LinkedList extd_types; 12 | 13 | public void addExtd(TypeExp_Idn par){ 14 | if(this.exts==null){ 15 | this.exts=new LinkedList(); 16 | } 17 | this.exts.add(par); 18 | } 19 | public boolean isE() { 20 | return isE; 21 | } 22 | public void setE() { 23 | this.isE = true; 24 | } 25 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 26 | return true; 27 | } 28 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 29 | if(isE){ 30 | return true; 31 | } 32 | this.extd_types=new LinkedList(); 33 | HashSet all_t=new HashSet(); 34 | for(TypeExp_Idn t:exts){ 35 | if(!t.genSymTb(codegen))return false; 36 | if(all_t.contains(t.rst_type)) 37 | throw new GenSymTblException("gensymtable error: "+t.rst_type); 38 | this.extd_types.add(t.rst_type); 39 | all_t.add(t.rst_type); 40 | } 41 | return true; 42 | } 43 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 44 | if(isE){ 45 | return true; 46 | } 47 | 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Cls_Impl_Lst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Cls_Impl_Lst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Cls_Impl_Lst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.TypeSys.GenCodeException; 7 | import Parser.TypeSys.GenSymTblException; 8 | import Parser.TypeSys.R_Function; 9 | import Parser.TypeSys.T_Class; 10 | import Parser.TypeSys.T_Interface; 11 | import Parser.TypeSys.T_Type; 12 | import Parser.TypeSys.TypeCheckException; 13 | 14 | public class Cls_Impl_Lst extends AST { 15 | boolean isE=true; 16 | LinkedList imps; 17 | LinkedList extd_types; 18 | 19 | public void addImp(TypeExp_Idn par){ 20 | if(this.imps==null){ 21 | this.imps=new LinkedList(); 22 | this.isE=false; 23 | } 24 | this.imps.add(par); 25 | } 26 | public boolean isE() { 27 | return isE; 28 | } 29 | public void setE() { 30 | this.isE = true; 31 | } 32 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 33 | 34 | return true; 35 | } 36 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 37 | if(isE){ 38 | return true; 39 | } 40 | this.extd_types=new LinkedList(); 41 | HashSet all_t=new HashSet(); 42 | for(TypeExp_Idn t:imps){ 43 | if(!t.genSymTb(codegen))return false; 44 | if(all_t.contains(t.rst_type)) 45 | return false; 46 | this.extd_types.add(t.rst_type); 47 | all_t.add(t.rst_type); 48 | } 49 | return true; 50 | } 51 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 52 | if(isE){ 53 | return true; 54 | } 55 | 56 | return true; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Expr.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | 6 | public class Expr extends AST { 7 | Expr_Lmbd lmbd; 8 | Expr_Calc calc; 9 | String rst_val;//return value of calculation 10 | String rst_type;//return type of calculation 11 | String ref_type;//reference type for assignment 12 | 13 | public boolean setExpr(AST ast){ 14 | switch(this.getASTType()){ 15 | case "Expr_Lmbd": 16 | this.lmbd=(Expr_Lmbd)ast; 17 | break; 18 | case "Expr_Calc": 19 | this.calc=(Expr_Calc)ast; 20 | break; 21 | default: 22 | break; 23 | } 24 | return true; 25 | } 26 | public String getRst(){ 27 | return this.rst_val; 28 | } 29 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 30 | switch(this.getASTType()){ 31 | case "Expr_Lmbd": 32 | this.lmbd.genCode(codegen); 33 | break; 34 | case "Expr_Calc": 35 | this.calc.genCode(codegen); 36 | break; 37 | default: 38 | return false; 39 | } 40 | return true; 41 | } 42 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 43 | switch(this.getASTType()){ 44 | case "Expr_Lmbd": 45 | if(!this.lmbd.genSymTb(codegen)) 46 | return false; 47 | this.rst_val=this.lmbd.rst_val; 48 | break; 49 | case "Expr_Calc": 50 | //this.calc.ref_type=this.ref_type; 51 | if(!this.calc.genSymTb(codegen)) 52 | return false; 53 | this.rst_val=this.calc.rst_val; 54 | break; 55 | default: 56 | return false; 57 | } 58 | return true; 59 | } 60 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 61 | switch(this.getASTType()){ 62 | case "Expr_Lmbd": 63 | this.lmbd.ref_type=this.ref_type; 64 | if(!this.lmbd.checkType(codegen)) 65 | return false; 66 | this.rst_type=this.lmbd.rst_type; 67 | break; 68 | case "Expr_Calc": 69 | this.calc.ref_type=this.ref_type; 70 | if(!this.calc.checkType(codegen)) 71 | return false; 72 | this.rst_type=this.calc.rst_type; 73 | break; 74 | default: 75 | return false; 76 | } 77 | return true; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/ExprAccs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/ExprAccs.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/ExprAccs.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.GenCodeException; 5 | import Parser.TypeSys.GenSymTblException; 6 | import Parser.TypeSys.TypeCheckException; 7 | 8 | public class ExprAccs extends AST { 9 | ExprAccs_Fld fld; 10 | ExprAccs_Pri pri; 11 | ExprAccs_Arr arr; 12 | ExprAccs_App app; 13 | String rst_val; 14 | String rst_type; 15 | String ref_type; 16 | 17 | public boolean setAccs(AST ast){ 18 | switch(this.getASTType()){ 19 | case "ExprAccs_Fld": 20 | this.fld=(ExprAccs_Fld)ast; 21 | break; 22 | case "ExprAccs_Pri": 23 | this.pri=(ExprAccs_Pri)ast; 24 | break; 25 | case "ExprAccs_Arr": 26 | this.arr=(ExprAccs_Arr)ast; 27 | break; 28 | case "ExprAccs_App": 29 | this.app=(ExprAccs_App)ast; 30 | break; 31 | default: 32 | break; 33 | } 34 | return true; 35 | } 36 | 37 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 38 | switch(this.getASTType()){ 39 | case "ExprAccs_Fld": 40 | this.fld.genCode(codegen); 41 | break; 42 | case "ExprAccs_Pri": 43 | this.pri.genCode(codegen); 44 | break; 45 | case "ExprAccs_Arr": 46 | this.arr.genCode(codegen); 47 | break; 48 | case "ExprAccs_App": 49 | this.app.genCode(codegen); 50 | break; 51 | default: 52 | break; 53 | } 54 | return true; 55 | } 56 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 57 | switch(this.getASTType()){ 58 | case "ExprAccs_Fld": 59 | if(!this.fld.genSymTb(codegen)) 60 | return false; 61 | this.rst_val=this.fld.rst_val; 62 | //this.rst_type=this.fld.rst_type; 63 | break; 64 | case "ExprAccs_Pri": 65 | if(!this.pri.genSymTb(codegen)) 66 | return false; 67 | //this.rst_type=this.pri.rst_type; 68 | this.rst_val=this.pri.rst_val; 69 | break; 70 | case "ExprAccs_Arr": 71 | if(!this.arr.genSymTb(codegen)) 72 | return false; 73 | //this.rst_type=this.arr.rst_type; 74 | this.rst_val=this.arr.rst_val; 75 | break; 76 | case "ExprAccs_App": 77 | if(!this.app.genSymTb(codegen)) 78 | return false; 79 | //this.rst_type=this.app.rst_type; 80 | this.rst_val=this.app.rst_val; 81 | break; 82 | default: 83 | break; 84 | } 85 | return true; 86 | } 87 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 88 | switch(this.getASTType()){ 89 | case "ExprAccs_Fld": 90 | this.fld.ref_type=this.ref_type; 91 | if(!this.fld.checkType(codegen)) 92 | return false; 93 | this.rst_type=this.fld.rst_type; 94 | break; 95 | case "ExprAccs_Pri": 96 | this.pri.ref_type=this.ref_type; 97 | if(!this.pri.checkType(codegen)) 98 | return false; 99 | this.rst_type=this.pri.rst_type; 100 | break; 101 | case "ExprAccs_Arr": 102 | this.arr.ref_type=this.ref_type; 103 | if(!this.arr.checkType(codegen)) 104 | return false; 105 | this.rst_type=this.arr.rst_type; 106 | break; 107 | case "ExprAccs_App": 108 | this.app.ref_type=this.ref_type; 109 | if(!this.app.checkType(codegen)) 110 | return false; 111 | this.rst_type=this.app.rst_type; 112 | break; 113 | default: 114 | break; 115 | } 116 | return true; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/ExprAccs_App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/ExprAccs_App.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/ExprAccs_Arr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/ExprAccs_Arr.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/ExprAccs_Arr.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class ExprAccs_Arr extends AST { 8 | ExprAccs_Fld pre_fld; 9 | NewArr_DimLst dim_lst; 10 | String rst_val; 11 | String ref_type; 12 | String rst_type; 13 | 14 | public void setAccs(ExprAccs_Fld pre_fld,NewArr_DimLst dim_lst){ 15 | this.pre_fld=pre_fld; 16 | this.dim_lst=dim_lst; 17 | } 18 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 19 | this.pre_fld.genCode(codegen); 20 | this.dim_lst.genCode(codegen); 21 | IRCode code =new IRCode("getArray",this.rst_val,this.pre_fld.rst_val,this.dim_lst.rst_val); 22 | codegen.addCode(code); 23 | return true; 24 | } 25 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 26 | if(!this.pre_fld.genSymTb(codegen)) 27 | return false; 28 | if(!this.dim_lst.genSymTb(codegen)) 29 | return false; 30 | //this.rst_type="["+codegen.getTmpSn(); 31 | this.rst_val="%"+codegen.getTmpSn(); 32 | T_Array t=new T_Array(); 33 | t.setKType(T_Type.KType.t_arr); 34 | t.setTypeName(this.rst_type); 35 | R_Variable r=new R_Variable(); 36 | r.setVarName(this.rst_val); 37 | r.setVarType(this.rst_type); 38 | codegen.putVarInSymTb(this.rst_val, r); 39 | codegen.putTypeInSymTb(this.rst_type, t); 40 | return true; 41 | } 42 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 43 | if(!this.pre_fld.checkType(codegen)) 44 | return false; 45 | if(!this.dim_lst.checkType(codegen)) 46 | return false; 47 | T_Type t1=codegen.getTypeInSymTb(this.pre_fld.rst_type); 48 | if(t1.getKType()!=T_Type.KType.t_arr) 49 | return false; 50 | int d1=((T_Array)t1).getDims();//pre_field's dimension 51 | if(d1"+this.ref_type,this.rst_val,this.accs.rst_val); 22 | codegen.addCode(code); 23 | return true; 24 | } 25 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 26 | if(!this.type_exp.genSymTb(codegen)) 27 | return false; 28 | if(!this.accs.genSymTb(codegen)) 29 | return false; 30 | this.rst_val="%"+codegen.getTmpSn(); 31 | R_Variable r=new R_Variable(); 32 | r.setVarName(this.rst_val); 33 | r.addRstVal(this.rst_val); 34 | this.rst_type=this.ref_type; 35 | r.setVarType(this.ref_type); 36 | codegen.putVarInSymTb(this.rst_val, r); 37 | return true; 38 | } 39 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 40 | if(!this.type_exp.checkType(codegen)) 41 | return false; 42 | if(!this.accs.checkType(codegen)) 43 | return false; 44 | if(!codegen.getTypeInSymTb(this.ref_type).canCastFrom(codegen, codegen.getTypeInSymTb(this.rst_type))) 45 | throw new TypeCheckException("type check error: cast "+this.rst_type); 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Calc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Expr_Calc.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Calc.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.GenCodeException; 5 | import Parser.TypeSys.GenSymTblException; 6 | import Parser.TypeSys.TypeCheckException; 7 | 8 | public class Expr_Calc extends AST { 9 | //cond_exp new_class_exp new_array_exp 10 | ExprCalc_Cond cond; 11 | ExprCalc_NewCls newCls; 12 | ExprCalc_NewArr newArr; 13 | String rst_type; 14 | String rst_val; 15 | String ref_type; 16 | 17 | public boolean setCalc(AST ast){ 18 | switch(this.getASTType()){ 19 | case "ExprCalc_Cond": 20 | this.cond=(ExprCalc_Cond)ast; 21 | break; 22 | case "ExprCalc_NewCls": 23 | this.newCls=(ExprCalc_NewCls)ast; 24 | break; 25 | case "ExprCalc_NewArr": 26 | this.newArr=(ExprCalc_NewArr)ast; 27 | break; 28 | default: 29 | break; 30 | } 31 | return true; 32 | } 33 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 34 | switch(this.getASTType()){ 35 | case "ExprCalc_Cond": 36 | this.cond.genCode(codegen); 37 | break; 38 | case "ExprCalc_NewCls": 39 | this.newCls.genCode(codegen); 40 | break; 41 | case "ExprCalc_NewArr": 42 | this.newArr.genCode(codegen); 43 | break; 44 | default:return false; 45 | } 46 | return true; 47 | } 48 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 49 | switch(this.getASTType()){ 50 | case "ExprCalc_Cond": 51 | //this.cond.ref_type=this.ref_type; 52 | if(!this.cond.genSymTb(codegen)) 53 | return false; 54 | this.rst_val=this.cond.rst_val; 55 | break; 56 | case "ExprCalc_NewCls": 57 | if(!this.newCls.genSymTb(codegen)) 58 | return false; 59 | this.rst_val=this.newCls.rst_val; 60 | break; 61 | case "ExprCalc_NewArr": 62 | if(!this.newArr.genSymTb(codegen)) 63 | return false; 64 | this.rst_val=this.newArr.rst_val; 65 | break; 66 | default: 67 | return false; 68 | } 69 | return true; 70 | } 71 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 72 | switch(this.getASTType()){ 73 | case "ExprCalc_Cond": 74 | this.cond.ref_type=this.ref_type; 75 | if(!this.cond.checkType(codegen)) 76 | return false; 77 | this.rst_type=this.cond.rst_type; 78 | break; 79 | case "ExprCalc_NewCls": 80 | this.newCls.ref_type=this.ref_type; 81 | if(!this.newCls.checkType(codegen)) 82 | return false; 83 | this.rst_type=this.newCls.rst_type; 84 | break; 85 | case "ExprCalc_NewArr": 86 | this.newArr.ref_type=this.ref_type; 87 | if(!this.newArr.checkType(codegen)) 88 | return false; 89 | this.rst_type=this.newArr.rst_type; 90 | break; 91 | default:return false; 92 | } 93 | return true; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Left.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Expr_Left.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Left.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | 6 | public class Expr_Left extends AST { 7 | ExprAccs_Fld acces_field; 8 | ExprAccs_Arr acces_array; 9 | String rst_addr; 10 | String ref_type; 11 | 12 | public boolean setAccs(AST ast){ 13 | switch(this.getASTType()){ 14 | case "ExprAccs_Fld": 15 | this.acces_field =(ExprAccs_Fld)ast; 16 | break; 17 | case "ExprAccs_Arr": 18 | this.acces_array = (ExprAccs_Arr)ast; 19 | break; 20 | default: 21 | return false; 22 | } 23 | return true; 24 | } 25 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 26 | switch(this.getASTType()){ 27 | case "ExprAccs_Fld": 28 | this.acces_field.genCode(codegen); 29 | this.rst_addr=this.acces_field.rst_val; 30 | break; 31 | case "ExprAccs_Arr": 32 | this.acces_array.genCode(codegen); 33 | this.rst_addr=this.acces_array.rst_val; 34 | break; 35 | default: 36 | return false; 37 | } 38 | return true; 39 | } 40 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 41 | switch(this.getASTType()){ 42 | case "ExprAccs_Fld": 43 | if(!this.acces_field.genSymTb(codegen)) 44 | return false; 45 | break; 46 | case "ExprAccs_Arr": 47 | if(!this.acces_array.genSymTb(codegen)) 48 | return false; 49 | break; 50 | default: 51 | return false; 52 | } 53 | return true; 54 | } 55 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 56 | switch(this.getASTType()){ 57 | case "ExprAccs_Fld": 58 | if(!this.acces_field.checkType(codegen)) return false; 59 | this.ref_type=this.acces_field.rst_type; 60 | break; 61 | case "ExprAccs_Arr": 62 | if(!this.acces_array.checkType(codegen)) return false; 63 | this.ref_type=this.acces_array.rst_type; 64 | break; 65 | default: 66 | return false; 67 | } 68 | return true; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Lmbd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Expr_Lmbd.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Expr_Lmbd.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.IR.*; 6 | import Parser.TypeSys.*; 7 | 8 | public class Expr_Lmbd extends AST { 9 | FuncDef_ParLst pars; 10 | AST_StmtList stmt_list; 11 | R_Function r_func; 12 | T_Function t_type; 13 | String name; 14 | String rst_val;//return value of calculation 15 | String rst_type;//return type of calculation 16 | String ref_type;//reference type for assignment 17 | 18 | public void setLmbd(FuncDef_ParLst par_lst,AST_StmtList stmt_list){ 19 | this.pars=par_lst; 20 | this.stmt_list=stmt_list; 21 | } 22 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 23 | codegen.pushBlock4Sym(this); 24 | int old_scp=codegen.getScope(); 25 | this.setScope(codegen.addScope("lambda")); 26 | IRCode code=new IRCode("defLambdaExp",this.rst_val,this.t_type.genFuncSig(codegen),null); 27 | codegen.addCode(code); 28 | if(!this.pars.isE){ 29 | this.pars.genCode(codegen); 30 | } 31 | if(!this.stmt_list.genCode(codegen)) 32 | return false; 33 | code=new IRCode("end",null, null, null); 34 | codegen.addCode(code); 35 | codegen.setScope(old_scp); 36 | codegen.popBlock4Sym(); 37 | return true; 38 | } 39 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 40 | this.name="#"+codegen.getTmpSn(); 41 | this.rst_val=this.name; 42 | this.rst_type="function"; 43 | this.r_func=new R_Function(); 44 | this.t_type=new T_Function(); 45 | codegen.putFuncInSymTb(this.rst_val, this.r_func); 46 | R_Variable rf=new R_Variable(); 47 | rf.setVarName(this.rst_val); 48 | rf.setVarType(this.rst_type); 49 | rf.addRstVal(this.rst_val); 50 | codegen.putVarInSymTb(rf.getVarName(), rf); 51 | codegen.pushBlock4Sym(this); 52 | int old_scp=codegen.getScope(); 53 | this.setScope(codegen.addScope("lambda")); 54 | this.r_func.setTypeT(this.t_type); 55 | this.r_func.setFuncName(this.name); 56 | if(!this.pars.isE()){ 57 | if(!this.pars.genSymTb(codegen)) 58 | return false; 59 | //this.t_type.setParTypes(this.pars.pars_type); 60 | this.r_func.setParsName(this.pars.pars_name); 61 | for(int i=0;i args; 13 | LinkedList arg_types; 14 | boolean isE=false; 15 | String rst_val; 16 | //int size; 17 | 18 | public boolean isE() { 19 | return isE; 20 | } 21 | public void setE() { 22 | this.isE = true; 23 | } 24 | public void addArg(Expr ast0){ 25 | if(this.args==null){ 26 | this.args=new LinkedList(); 27 | } 28 | this.args.add(ast0); 29 | } 30 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 31 | //code=new IRCode("pushFArgs",this.arg_lst.rst_val,null,null); 32 | //codegen.addCode(code); 33 | if(this.isE) 34 | return true; 35 | IRCode code=null; 36 | for(Expr exp :this.args){ 37 | exp.genCode(codegen); 38 | //code=new IRCode("pushFuncArg", exp.rst_val,null,null); 39 | //codegen.addCode(code); 40 | } 41 | return true; 42 | } 43 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 44 | if(this.isE) 45 | return true; 46 | this.rst_val=""; 47 | for(Expr exp:this.args){ 48 | if(!exp.genSymTb(codegen)) 49 | return false; 50 | this.rst_val+=exp.rst_val+","; 51 | //this.size++; 52 | } 53 | this.rst_val=this.rst_val.substring(0,this.rst_val.length()-1); 54 | return true; 55 | } 56 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 57 | if(this.isE) 58 | return true; 59 | if(this.arg_types==null) this.arg_types=new LinkedList(); 60 | for(Expr exp:this.args){ 61 | if(!exp.checkType(codegen)) 62 | return false; 63 | this.arg_types.add(exp.rst_type); 64 | } 65 | return true; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/FuncDef_Par.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/FuncDef_Par.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/FuncDef_Par.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class FuncDef_Par extends AST { 8 | TypeExp type; 9 | ExprPri_Var var; 10 | public FuncDef_Par(TypeExp type,ExprPri_Var var){ 11 | this.type=type; 12 | this.var=var; 13 | } 14 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 15 | IRCode code=new IRCode("defFuncPar", this.type.rst_type, this.var.rst_val, null); 16 | codegen.addCode(code); 17 | return true; 18 | } 19 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 20 | if(!this.type.genSymTb(codegen)) 21 | return false; 22 | if(!this.var.genSymTb(codegen)) 23 | return false; 24 | return true; 25 | } 26 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 27 | if(!this.type.checkType(codegen)) 28 | return false; 29 | if(!this.var.checkType(codegen)) 30 | return false; 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/FuncDef_ParLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/FuncDef_ParLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/FuncDef_ParLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class FuncDef_ParLst extends AST { 10 | boolean isE=false; 11 | LinkedList pars; 12 | LinkedList pars_type; 13 | LinkedList pars_name; 14 | //String rst_val; 15 | 16 | 17 | public void addPar(FuncDef_Par par){ 18 | if(pars==null){ 19 | this.pars=new LinkedList(); 20 | } 21 | this.pars.add(par); 22 | } 23 | public boolean isE() { 24 | return isE; 25 | } 26 | public void setE() { 27 | this.isE = true; 28 | } 29 | //public String getVal() { 30 | // return rst_val; 31 | //} 32 | //public void setVal(String val) { 33 | // this.rst_val = val; 34 | //} 35 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 36 | if(isE) 37 | return true; 38 | for(FuncDef_Par par:pars){ 39 | par.genCode(codegen); 40 | //this.rst_val+=par.var.rst_val+","; 41 | } 42 | //this.rst_val=this.rst_val.substring(0, rst_val.length()-1); 43 | return true; 44 | } 45 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 46 | if(isE) 47 | return true; 48 | HashSet names=new HashSet(); 49 | this.pars_type=new LinkedList(); 50 | this.pars_name=new LinkedList(); 51 | for(FuncDef_Par par:pars){ 52 | if(!par.genSymTb(codegen)) 53 | return false; 54 | if(names.contains(par.var.name)) 55 | throw new GenSymTblException("gensymtable error: existing var "+par.var.name); 56 | names.add(par.var.name); 57 | this.pars_name.add(par.var.name); 58 | } 59 | return true; 60 | } 61 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 62 | if(isE) 63 | return true; 64 | for(FuncDef_Par par:pars){ 65 | if(!par.checkType(codegen)) 66 | return false; 67 | this.pars_type.add(par.type.rst_type); 68 | codegen.putTypeInAllFileTb(par.type.rst_type, codegen.getTypeInSymTb(par.type.rst_type)); 69 | } 70 | return true; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_ArgLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Gnrc_ArgLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_ArgLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.IR.IRCode; 7 | import Parser.TypeSys.*; 8 | 9 | public class Gnrc_ArgLst extends AST { 10 | Gnrc_ArgLst pre_args; 11 | TypeExp var; 12 | TypeExp_Idn ext_idn_t; 13 | LinkedListtypes_name; 14 | String rst_val; 15 | int size; 16 | 17 | public boolean setGnrcArgs(Gnrc_ArgLst pre_args,TypeExp var,TypeExp_Idn idn_type){ 18 | this.pre_args=pre_args; 19 | this.var=var; 20 | this.ext_idn_t=idn_type; 21 | return true; 22 | } 23 | 24 | public LinkedList getTypesName() { 25 | return types_name; 26 | } 27 | public void setTypesName(LinkedList gnrc_args) { 28 | this.types_name = gnrc_args; 29 | } 30 | /* public String getArgsSig() { 31 | return rst_val; 32 | } 33 | public void setArgsSig(String arg_types) { 34 | this.rst_val = arg_types; 35 | } 36 | */ 37 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 38 | //IRCode code=null; 39 | 40 | return true; 41 | } 42 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 43 | if(this.pre_args!=null&&!this.pre_args.genSymTb(codegen))return false; 44 | if(!this.var.genSymTb(codegen))return false; 45 | return true; 46 | } 47 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 48 | if(this.pre_args!=null&&!this.pre_args.checkType(codegen)) 49 | return false; 50 | if(!this.var.checkType(codegen)) 51 | return false; 52 | if(codegen.getTypeInSymTb(this.var.rst_type)==null) 53 | throw new TypeCheckException("type error: func gnrc-arg not found "+this.var.rst_type); 54 | if(this.pre_args!=null){ 55 | this.types_name=this.pre_args.types_name; 56 | this.types_name.add(var.rst_type); 57 | }else{ 58 | this.types_name=new LinkedList(); 59 | this.types_name.add(var.rst_type); 60 | } 61 | String s=""; 62 | Iterator it=this.types_name.iterator(); 63 | while(it.hasNext()){ 64 | String s1=it.next(); 65 | if(codegen.getTypeInSymTb(s1)==null) 66 | throw new TypeCheckException("type error: func gnrc-arg not found "+s1); 67 | s+=s1+","; 68 | } 69 | this.rst_val=s.substring(0,s.length()-1); 70 | return true; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_Par.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Gnrc_Par.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_Par.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.AST; 4 | import Parser.CodeGenerator; 5 | import Parser.IR.IRCode; 6 | import Parser.TypeSys.GenCodeException; 7 | import Parser.TypeSys.GenSymTblException; 8 | import Parser.TypeSys.TypeCheckException; 9 | 10 | public class Gnrc_Par extends AST { 11 | ExprPri_Var var; 12 | TypeExp_Idn idn;//no use, will add in future 13 | String rst_val; 14 | public Gnrc_Par(ExprPri_Var var){ 15 | this.var=var; 16 | } 17 | public Gnrc_Par(ExprPri_Var var,TypeExp_Idn idn){ 18 | this.var=var; 19 | this.idn=idn; 20 | } 21 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 22 | IRCode code=new IRCode("defGnrcPar", this.var.rst_val, null, null); 23 | codegen.addCode(code); 24 | return true; 25 | } 26 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 27 | if(!this.var.genSymTb(codegen)) 28 | return false; 29 | return true; 30 | } 31 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 32 | 33 | return true; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_ParLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Gnrc_ParLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Gnrc_ParLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class Gnrc_ParLst extends AST { 8 | boolean isE=false; 9 | LinkedList g_pars; 10 | LinkedList types_name; 11 | //String rst_val; 12 | 13 | public void addPar(Gnrc_Par par){ 14 | if(g_pars==null){ 15 | this.g_pars=new LinkedList(); 16 | } 17 | this.g_pars.add(par); 18 | } 19 | public boolean isE() { 20 | return isE; 21 | } 22 | public void setE() { 23 | this.isE = true; 24 | } 25 | //public String getVal() { 26 | // return rst_val; 27 | //} 28 | //public void setVal(String val) { 29 | // this.rst_val = val; 30 | //} 31 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 32 | //gen rst_val 33 | if(isE) 34 | return true; 35 | for(Gnrc_Par par:this.g_pars){ 36 | par.genCode(codegen); 37 | //this.rst_val+=par.rst_val+","; 38 | } 39 | //this.rst_val=this.rst_val.substring(0,this.rst_val.length()-1); 40 | return true; 41 | } 42 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 43 | if(isE) 44 | return true; 45 | this.types_name=new LinkedList(); 46 | HashSet all_ns=new HashSet(); 47 | for(Gnrc_Par p:g_pars){ 48 | if(!p.genSymTb(codegen)) 49 | return false; 50 | this.types_name.add(p.var.name); 51 | if(codegen.getTypeInSymTb(p.var.name)!=null) 52 | throw new GenSymTblException("gensymtable error: gnrc par type "+p.var.name); 53 | if(all_ns.contains(p.var.name)) 54 | throw new GenSymTblException("gensymtable error: gnrc par type "+p.var.name); 55 | T_Type t=new T_Type(); 56 | t.setTypeName(p.var.name); 57 | t.setDummy(); 58 | codegen.putTypeInSymTb(p.var.name, t); 59 | all_ns.add(p.var.name); 60 | } 61 | return true; 62 | } 63 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 64 | 65 | return true; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/MbrDef.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.TypeSys.*; 7 | 8 | public class MbrDef extends AST { 9 | MbrDef_Fld fld; 10 | MbrDef_Mthd mthd; 11 | LinkedList r_vars; 12 | R_Function r_func; 13 | 14 | public MbrDef(){} 15 | public MbrDef(MbrDef_Fld fld){ 16 | this.fld=fld; 17 | this.setASTType(MbrDef_Fld.class.getName()); 18 | } 19 | public MbrDef(MbrDef_Mthd mthd){ 20 | this.mthd=mthd; 21 | this.setASTType(MbrDef_Mthd.class.getName()); 22 | } 23 | public boolean setMbr(AST ast){ 24 | switch(this.getASTType()){ 25 | case "MbrDef_Fld": 26 | this.fld=(MbrDef_Fld)ast; 27 | break; 28 | case "MbrDef_Mthd": 29 | this.mthd=(MbrDef_Mthd)ast; 30 | break; 31 | } 32 | return true; 33 | } 34 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 35 | this.setScope(codegen.getScope()); 36 | switch(this.getASTType()){ 37 | case "MbrDef_Fld": 38 | if(!this.fld.genCode(codegen)) 39 | return false; 40 | break; 41 | case "MbrDef_Mthd": 42 | if(!this.mthd.genCode(codegen)) 43 | return false; 44 | break; 45 | default:return false; 46 | } 47 | return true; 48 | } 49 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 50 | switch(this.getASTType()){ 51 | case "MbrDef_Fld": 52 | if(!this.fld.genSymTb(codegen)) 53 | return false; 54 | this.r_vars=this.fld.var_def.r_vars; 55 | break; 56 | case "MbrDef_Mthd": 57 | if(!this.mthd.genSymTb(codegen)) 58 | return false; 59 | this.r_func=this.mthd.func_def.r_func; 60 | break; 61 | default:return false; 62 | } 63 | return true; 64 | } 65 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 66 | switch(this.getASTType()){ 67 | case "MbrDef_Fld": 68 | if(!this.fld.checkType(codegen)) return false; 69 | break; 70 | case "MbrDef_Mthd": 71 | if(!this.mthd.checkType(codegen)) return false; 72 | break; 73 | default:return false; 74 | } 75 | return true; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Fld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/MbrDef_Fld.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Fld.java: -------------------------------------------------------------------------------- 1 | 2 | package Parser.ASTs; 3 | 4 | import java.util.*; 5 | 6 | import Parser.*; 7 | import Parser.TypeSys.GenCodeException; 8 | import Parser.TypeSys.GenSymTblException; 9 | import Parser.TypeSys.TypeCheckException; 10 | 11 | public class MbrDef_Fld extends AST { 12 | Scp_InfoLst scps; 13 | SgStmt_DefVar var_def; 14 | 15 | public boolean setFld(Scp_InfoLst scps,SgStmt_DefVar var_def){ 16 | this.scps=scps; 17 | this.var_def=var_def; 18 | if(var_def.getMergedAsts()!=null){ 19 | return false; 20 | } 21 | return true; 22 | } 23 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 24 | 25 | return true; 26 | } 27 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 28 | if(this.scps!=null){ 29 | if(!this.scps.genSymTb(codegen)) 30 | return false; 31 | } 32 | if(this.var_def!=null){ 33 | if(!this.var_def.genSymTb(codegen)) 34 | return false; 35 | } 36 | return true; 37 | } 38 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 39 | if(this.scps!=null){ 40 | if(!this.scps.checkType(codegen)) 41 | return false; 42 | } 43 | if(this.var_def!=null){ 44 | if(!this.var_def.checkType(codegen)) 45 | return false; 46 | } 47 | return true; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Lst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/MbrDef_Lst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Lst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.TypeSys.*; 7 | 8 | public class MbrDef_Lst extends AST { 9 | boolean isE=false; 10 | LinkedList mbrs; 11 | LinkedList fields; 12 | LinkedList methods; 13 | 14 | public void addMbr(MbrDef par){ 15 | if(this.mbrs==null){ 16 | this.mbrs=new LinkedList(); 17 | this.fields=new LinkedList(); 18 | this.methods=new LinkedList(); 19 | } 20 | this.mbrs.add(par); 21 | } 22 | public boolean isE() { 23 | return isE; 24 | } 25 | public void setE() { 26 | this.isE = true; 27 | } 28 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 29 | if(isE)return true; 30 | for(MbrDef mbr:mbrs){ 31 | if(!mbr.genCode(codegen)) 32 | return false; 33 | } 34 | return true; 35 | } 36 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 37 | this.setScope(codegen.getScope()); 38 | if(isE)return true; 39 | for(MbrDef mbr:mbrs){ 40 | if(!mbr.genSymTb(codegen)) 41 | return false; 42 | if(mbr.getASTType().equals("MbrDef_Fld")){ 43 | this.fields.addAll(mbr.r_vars); 44 | }else if(mbr.getASTType().equals("MbrDef_Mthd")){ 45 | this.methods.add(mbr.r_func); 46 | }else 47 | return false; 48 | } 49 | return true; 50 | } 51 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 52 | if(isE)return true; 53 | for(MbrDef mbr:mbrs){ 54 | if(!mbr.checkType(codegen)) 55 | return false; 56 | // if(mbr.getASTType().equals("MbrDef_Fld")){ 57 | // this.fields.addAll(mbr.r_vars); 58 | // }else if(mbr.getASTType().equals("MbrDef_Mthd")){ 59 | // this.methods.add(mbr.r_func); 60 | // }else 61 | // return false; 62 | } 63 | return true; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Mthd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/MbrDef_Mthd.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/MbrDef_Mthd.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.GenCodeException; 5 | import Parser.TypeSys.GenSymTblException; 6 | import Parser.TypeSys.TypeCheckException; 7 | 8 | public class MbrDef_Mthd extends AST { 9 | Scp_InfoLst scps; 10 | Stmt_DefFunc func_def; 11 | 12 | public boolean setMthd(Scp_InfoLst scps,Stmt_DefFunc func_def){ 13 | this.scps=scps; 14 | this.func_def=func_def; 15 | /* if(func_def.getMergedAsts()!=null){ 16 | return false; 17 | } 18 | for(String s:func_def.getFuncUp()){ 19 | if(this.getFuncTb()!=null && this.getFuncTb().keySet().contains(s)){ 20 | System.out.println("error existing symbol name: "+ s); 21 | }else{ 22 | this.putFuncTb(s, func_def.getFuncTb().get(s)); 23 | } 24 | }*/ 25 | return true; 26 | } 27 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 28 | this.func_def.genCode(codegen); 29 | return true; 30 | } 31 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 32 | this.setScope(codegen.getScope()); 33 | if(this.scps!=null&&!this.scps.genSymTb(codegen)) 34 | return false; 35 | if(this.func_def!=null){ 36 | if(!this.func_def.genSymTb(codegen)) 37 | return false; 38 | } 39 | return true; 40 | } 41 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 42 | if(this.scps!=null&&!this.scps.checkType(codegen)) 43 | return false; 44 | if(this.func_def!=null&&!this.func_def.checkType(codegen)) 45 | return false; 46 | return true; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_DimLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/NewArr_DimLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_DimLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class NewArr_DimLst extends AST { 10 | LinkedList dims; 11 | String rst_val=""; 12 | int dim_n=0; 13 | 14 | public boolean addDim(ExprCalc_Add ast){ 15 | if(this.dims==null){ 16 | this.dims=new LinkedList(); 17 | } 18 | this.dims.add(ast); 19 | return true; 20 | } 21 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 22 | for(ExprCalc_Add dim:dims){ 23 | dim.genCode(codegen); 24 | } 25 | return true; 26 | } 27 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 28 | for(ExprCalc_Add dim:dims){ 29 | if(!dim.genSymTb(codegen)) 30 | return false; 31 | dim.ref_type="int"; 32 | this.rst_val+=dim.rst_val+","; 33 | this.dim_n++; 34 | } 35 | this.rst_val=this.rst_val.substring(0,this.rst_val.length()-1); 36 | return true; 37 | } 38 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 39 | for(ExprCalc_Add dim:dims){ 40 | if(!dim.checkType(codegen)) 41 | return false; 42 | } 43 | return true; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_Init.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/NewArr_Init.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_Init.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.GenCodeException; 5 | import Parser.TypeSys.GenSymTblException; 6 | import Parser.TypeSys.TypeCheckException; 7 | 8 | public class NewArr_Init extends AST { 9 | Expr_Calc calc; 10 | NewArr_InitLst lst; 11 | 12 | public void setCalc(Expr_Calc calc) { 13 | this.calc = calc; 14 | } 15 | public void setLst(NewArr_InitLst lst) { 16 | this.lst = lst; 17 | } 18 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 19 | 20 | return true; 21 | } 22 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 23 | 24 | return true; 25 | } 26 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 27 | 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_InitLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/NewArr_InitLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/NewArr_InitLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class NewArr_InitLst extends AST { 10 | LinkedList init_lst; 11 | String rst_val; 12 | String rst_type; 13 | 14 | public boolean addInit(NewArr_Init ast){ 15 | if(this.init_lst==null){ 16 | this.init_lst=new LinkedList(); 17 | } 18 | this.init_lst.add(ast); 19 | return true; 20 | } 21 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 22 | return true; 23 | } 24 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 25 | //new type, new var, new function, put in table 26 | return true; 27 | } 28 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 29 | return true; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Scp_Info$en_Scp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Scp_Info$en_Scp.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Scp_Info.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Scp_Info.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Scp_Info.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.AST; 4 | import Parser.CodeGenerator; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class Scp_Info extends AST { 10 | en_Scp scp; 11 | public en_Scp getScp() { 12 | return scp; 13 | } 14 | public void setScp(en_Scp scp) { 15 | this.scp = scp; 16 | } 17 | public enum en_Scp{t_static,t_public,t_private,t_final;} 18 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 19 | 20 | return true; 21 | } 22 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 23 | 24 | return true; 25 | } 26 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 27 | 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Scp_InfoLst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Scp_InfoLst.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Scp_InfoLst.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.ASTs.Scp_Info.en_Scp; 6 | import Parser.TypeSys.GenCodeException; 7 | import Parser.TypeSys.GenSymTblException; 8 | import Parser.TypeSys.TypeCheckException; 9 | 10 | public class Scp_InfoLst extends AST { 11 | boolean isE=false; 12 | public boolean isE() { 13 | return isE; 14 | } 15 | public void setE() { 16 | this.isE = true; 17 | } 18 | LinkedList scp_infos; 19 | public void addScp(Scp_Info scp){ 20 | if(this.scp_infos==null){ 21 | this.scp_infos=new LinkedList(); 22 | } 23 | this.scp_infos.add(scp); 24 | } 25 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 26 | 27 | return true; 28 | } 29 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 30 | 31 | return true; 32 | } 33 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 34 | 35 | return true; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_AsgnVar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/SgStmt_AsgnVar.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_AsgnVar.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class SgStmt_AsgnVar extends AST { 8 | Expr_Left left_hand; 9 | Expr expr; 10 | 11 | public Expr_Left getLeft() { 12 | return left_hand; 13 | } 14 | public void setLeft(Expr_Left left_hand) { 15 | this.left_hand = left_hand; 16 | } 17 | public Expr getExpr() { 18 | return expr; 19 | } 20 | public void setExpr(Expr expr) { 21 | this.expr = expr; 22 | } 23 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 24 | this.left_hand.genCode(codegen); 25 | this.expr.genCode(codegen); 26 | IRCode code; 27 | code=new IRCode("mov",this.left_hand.ref_type,this.left_hand.rst_addr,this.expr.getRst()); 28 | codegen.addCode(code); 29 | return true; 30 | } 31 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 32 | if(!this.left_hand.genSymTb(codegen)) 33 | return false; 34 | if(!this.expr.genSymTb(codegen)) 35 | return false; 36 | R_Variable r=codegen.getVarInSymTb(this.expr.rst_val); 37 | R_Variable nr=new R_Variable(); 38 | nr.setVarName(this.left_hand.rst_addr); 39 | if(r!=null) nr.getRstVal().addAll(r.getRstVal()); 40 | else nr.addRstVal(this.expr.rst_val); 41 | codegen.putVarInSymTb(this.left_hand.rst_addr,nr); 42 | return true; 43 | } 44 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 45 | if(!this.left_hand.checkType(codegen)) 46 | return false; 47 | this.expr.ref_type=this.left_hand.ref_type; 48 | codegen.getVarInSymTb(this.left_hand.rst_addr).setVarType(this.left_hand.ref_type); 49 | if(!this.expr.checkType(codegen))return false; 50 | 51 | 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_CtrFlw$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/SgStmt_CtrFlw$1.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_CtrFlw$en_Ctrflw.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/SgStmt_CtrFlw$en_Ctrflw.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_CtrFlw.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/SgStmt_CtrFlw.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_CtrFlw.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class SgStmt_CtrFlw extends AST { 8 | en_Ctrflw t_ctrflw; 9 | Expr return_exp; 10 | 11 | public en_Ctrflw getCFT() { 12 | return t_ctrflw; 13 | } 14 | 15 | public void setCFT(en_Ctrflw t_ctrflw) { 16 | this.t_ctrflw = t_ctrflw; 17 | } 18 | 19 | public Expr getRtExp() { 20 | return return_exp; 21 | } 22 | 23 | public void setRtExp(Expr return_exp) { 24 | this.return_exp = return_exp; 25 | } 26 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 27 | switch(this.t_ctrflw){ 28 | case t_break: 29 | String lb_end=codegen.labels_whlend.peek(), lb_st=codegen.labels_whlbg.peek(); 30 | if(lb_end==null)throw new GenCodeException("GenCode Error: not in while range"); 31 | IRCode code=new IRCode("goto",lb_end,null,null); 32 | codegen.addCode(code); 33 | codegen.getRpsLst(lb_st).add(code); 34 | break; 35 | case t_continue: 36 | lb_st=codegen.labels_whlbg.peek(); 37 | if(lb_st==null)throw new GenCodeException("GenCode Error: not in while range"); 38 | code=new IRCode("goto",lb_st,null,null); 39 | codegen.addCode(code); 40 | codegen.getRpsLst(lb_st).add(code); 41 | 42 | break; 43 | case t_return: 44 | code =new IRCode("ret",null,null,null); 45 | codegen.addCode(code); 46 | break; 47 | case t_returnExp: 48 | this.return_exp.genCode(codegen); 49 | code =new IRCode("retExp",this.return_exp.rst_type,this.return_exp.rst_val,null); 50 | codegen.addCode(code); 51 | break; 52 | default:return false; 53 | } 54 | return true; 55 | } 56 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 57 | if(this.t_ctrflw==en_Ctrflw.t_returnExp){ 58 | if(!this.return_exp.genSymTb(codegen)) return false; 59 | } 60 | return true; 61 | } 62 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 63 | switch(this.t_ctrflw){ 64 | //case t_break: 65 | // if(!codegen.labels_whlend.isEmpty()) 66 | // return true; 67 | //else throw new TypeCheckException("Check Type Error: no while break lable"); 68 | //case t_continue: 69 | // if(!codegen.labels_whlbg.isEmpty()) 70 | // return true; 71 | // else throw new TypeCheckException("Check Type Error: no while continue lable"); 72 | case t_return: 73 | if(codegen.isInScope("function") 74 | &&!codegen.isInScope("lambda") 75 | &&codegen.ret_types.peek().equals("void")) 76 | return true; 77 | if(codegen.isInScope("lambda")){ 78 | //T_Type t=codegen.getTypeInSymTb("void"); 79 | codegen.ret_types.addFirst("void"); 80 | return true; 81 | } 82 | else throw new TypeCheckException("Check Type Error: return type is not void"); 83 | case t_returnExp: 84 | 85 | this.return_exp.ref_type=codegen.ret_types.peek(); 86 | if(!this.return_exp.checkType(codegen)) return false; 87 | if(codegen.isInScope("lambda")){ 88 | codegen.ret_types.addFirst(this.return_exp.rst_type); 89 | return true; 90 | } 91 | else if(codegen.isInScope("function")){ 92 | T_Type t1=codegen.getTypeInSymTb(codegen.ret_types.peek()) 93 | ,t2=codegen.getTypeInSymTb(this.return_exp.rst_type); 94 | if(t1.canCastFrom(codegen, t2)){ 95 | return true; 96 | } 97 | else throw new TypeCheckException("Check Type Error: return type mismatch"); 98 | } 99 | //else if(){ 100 | // 101 | //} 102 | else throw new TypeCheckException("Check Type Error: return type mismatch"); 103 | default:break; 104 | } 105 | return true; 106 | } 107 | public enum en_Ctrflw{t_break,t_continue,t_return,t_returnExp;} 108 | } 109 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_DefVar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/SgStmt_DefVar.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/SgStmt_DefVar.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.IR.*; 7 | import Parser.TypeSys.*; 8 | 9 | public class SgStmt_DefVar extends AST { 10 | SgStmt_DefVar pre_def; 11 | TypeExp type_exp; 12 | ExprPri_Var var; 13 | Expr expr; 14 | LinkedList r_vars; 15 | String ref_type; 16 | 17 | public void setPredef(SgStmt_DefVar var_def) { 18 | this.pre_def = var_def; 19 | this.type_exp=var_def.type_exp; 20 | } 21 | public void setTypeExp(TypeExp type_exp) { 22 | this.type_exp = type_exp; 23 | } 24 | public void setVar(ExprPri_Var var) { 25 | this.var = var; 26 | } 27 | public void setExpr(Expr expression) { 28 | this.expr = expression; 29 | } 30 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ //if only def var without assignment, just register on RT 31 | if(this.pre_def!=null){ 32 | this.pre_def.genCode(codegen); 33 | } 34 | if(this.var!=null){ 35 | if(this.expr!=null){ 36 | this.expr.genCode(codegen); 37 | IRCode code=new IRCode("mov",this.ref_type,this.var.rst_val,this.expr.rst_val); 38 | codegen.addCode(code); 39 | }else{ 40 | IRCode code=new IRCode("mov",this.ref_type,this.var.rst_val,"null"); 41 | codegen.addCode(code); 42 | } 43 | } 44 | return true; 45 | } 46 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 47 | this.setScope(codegen.getScope()); 48 | if(this.pre_def!=null){ 49 | if(!this.pre_def.genSymTb(codegen)) 50 | return false; 51 | this.r_vars=this.pre_def.r_vars; 52 | } 53 | else{ 54 | if(!this.type_exp.genSymTb(codegen)) 55 | return false; 56 | this.ref_type=this.type_exp.rst_type; 57 | this.r_vars=new LinkedList(); 58 | } 59 | if(codegen.getVarTopSymTb(this.var.name)!=null) 60 | throw new GenSymTblException("Error: var existed "+this.var.name); 61 | R_Variable r=new R_Variable(); 62 | 63 | r.setVarName(this.var.name); 64 | r.addRstVal(this.var.rst_val); 65 | this.var.ref_type=this.ref_type; 66 | //r.setVarType(this.ref_type); 67 | codegen.putVarInSymTb(this.var.name, r); 68 | this.r_vars.add(r); 69 | if(this.expr!=null){ 70 | if(!this.expr.genSymTb(codegen)) 71 | return false; 72 | r.addRstVal(this.expr.rst_val); 73 | } 74 | return true; 75 | } 76 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ //set var's type and expr's asgn_type 77 | if(this.pre_def!=null){ 78 | if(!this.pre_def.checkType(codegen)) 79 | return false; 80 | this.ref_type=this.pre_def.ref_type; 81 | R_Variable r=codegen.getVarInSymTb(this.var.name); 82 | if(r.getVarType()==null) 83 | r.setVarType(this.ref_type); 84 | this.var.ref_type=this.ref_type; 85 | } 86 | else{//this.pre_def!=null 87 | if(!this.type_exp.checkType(codegen)) 88 | return false; 89 | this.ref_type=this.type_exp.rst_type; 90 | this.var.ref_type=this.ref_type; 91 | R_Variable r=codegen.getVarInSymTb(this.var.name); 92 | r.setVarType(this.ref_type); 93 | } 94 | 95 | if(this.expr!=null){ 96 | this.expr.ref_type=this.ref_type; 97 | if(!this.expr.checkType(codegen)) 98 | return false; 99 | } 100 | 101 | return true; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/StmtIf_ElsBd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/StmtIf_ElsBd.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/StmtIf_ElsBd.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class StmtIf_ElsBd extends AST { 8 | Stmt_If if_stmt; 9 | AST_StmtList stmt_list; 10 | Stmt_Sg sg_stmt; 11 | 12 | public boolean setElsBd(Stmt_If if_stmt,AST_StmtList stmt_list,Stmt_Sg sg_stmt){ 13 | this.if_stmt=if_stmt; 14 | this.stmt_list=stmt_list; 15 | this.sg_stmt=sg_stmt; 16 | return true; 17 | } 18 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 19 | String lb_ifend=":"+String.valueOf(codegen.getTmpSn()); 20 | IRCode code=new IRCode("goto",lb_ifend,null,null); 21 | codegen.getRpsLst(lb_ifend).add(code); 22 | codegen.addCode(code); 23 | if(this.if_stmt!=null){ 24 | this.if_stmt.genCode(codegen); 25 | } 26 | if(this.stmt_list!=null){ 27 | this.stmt_list.genCode(codegen); 28 | } 29 | if(this.sg_stmt!=null){ 30 | this.sg_stmt.genCode(codegen); 31 | } 32 | int ln_ifend=codegen.getLineNo()+1; 33 | codegen.mp_label2line.put(lb_ifend, ln_ifend); 34 | codegen.replaceLb(lb_ifend); 35 | codegen.mp_label2line.remove(lb_ifend); 36 | codegen.rps_code_list.remove(lb_ifend); 37 | return true; 38 | } 39 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 40 | if(this.if_stmt!=null) 41 | return this.if_stmt.genSymTb(codegen); 42 | if(this.stmt_list!=null) 43 | return this.stmt_list.genSymTb(codegen); 44 | if(this.sg_stmt!=null) 45 | return this.sg_stmt.genSymTb(codegen); 46 | return false; 47 | } 48 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 49 | if(this.if_stmt!=null) 50 | return this.if_stmt.checkType(codegen); 51 | if(this.stmt_list!=null) 52 | return this.stmt_list.checkType(codegen); 53 | if(this.sg_stmt!=null) 54 | return this.sg_stmt.checkType(codegen); 55 | return false; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/StmtIf_IfBd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/StmtIf_IfBd.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/StmtIf_IfBd.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.IR.*; 7 | import Parser.TypeSys.GenCodeException; 8 | import Parser.TypeSys.GenSymTblException; 9 | import Parser.TypeSys.TypeCheckException; 10 | 11 | public class StmtIf_IfBd extends AST { 12 | ExprCalc_Bool bool_exp; 13 | AST_StmtList stmt_list; 14 | Stmt_Sg sg_stmt; 15 | String labels_ifbd; 16 | String labels_elsbd; 17 | 18 | public boolean setIfBd(ExprCalc_Bool bool_exp,AST_StmtList stmt_list,Stmt_Sg sg_stmt){ 19 | this.bool_exp=bool_exp; 20 | this.stmt_list=stmt_list; 21 | this.sg_stmt=sg_stmt; 22 | return true; 23 | } 24 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 25 | this.bool_exp.genCode(codegen); 26 | IRCode code=new IRCode("if",this.bool_exp.getVal(),this.labels_ifbd,this.labels_elsbd); 27 | codegen.addCode(code); 28 | int ln_ifbd=codegen.getLineNo()+1; 29 | codegen.mp_label2line.put(this.labels_ifbd, ln_ifbd); 30 | codegen.getRpsLst(this.labels_ifbd).add(code); 31 | //codegen.getRpsLst(codegen.labels_elsbd.peek()).add(code); 32 | if(this.sg_stmt!=null){ 33 | this.sg_stmt.genCode(codegen); 34 | }else if(this.stmt_list!=null){ 35 | this.stmt_list.genCode(codegen); 36 | } 37 | return true; 38 | } 39 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 40 | if(!this.bool_exp.genSymTb(codegen)){ 41 | return false; 42 | } 43 | if(this.sg_stmt!=null){ 44 | return this.sg_stmt.genSymTb(codegen); 45 | } 46 | else if(this.stmt_list!=null){ 47 | return this.stmt_list.genSymTb(codegen); 48 | } 49 | return true; 50 | } 51 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 52 | if(!this.bool_exp.checkType(codegen)){ 53 | return false; 54 | } 55 | if(this.sg_stmt!=null){ 56 | return this.sg_stmt.checkType(codegen); 57 | } 58 | else if(this.stmt_list!=null){ 59 | return this.stmt_list.checkType(codegen); 60 | } 61 | return false; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_DefCls.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_DefCls.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_DefFunc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_DefFunc.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_DefIntf.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_DefIntf.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_If.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_If.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_If.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.GenCodeException; 5 | import Parser.TypeSys.GenSymTblException; 6 | import Parser.TypeSys.TypeCheckException; 7 | 8 | public class Stmt_If extends AST { 9 | StmtIf_IfBd if_body; 10 | StmtIf_ElsBd else_body; 11 | 12 | public boolean setIfStmt(StmtIf_IfBd if_body,StmtIf_ElsBd else_body){ 13 | this.if_body=if_body; 14 | this.else_body=else_body; 15 | return true; 16 | } 17 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 18 | String lb_ifbd=":"+String.valueOf(codegen.getTmpSn()); 19 | this.if_body.labels_ifbd=lb_ifbd; 20 | String lb_elsbd=":"+String.valueOf(codegen.getTmpSn()); 21 | this.if_body.labels_elsbd=lb_elsbd; 22 | this.if_body.genCode(codegen); 23 | int ln_elsbd=codegen.getLineNo()+1; 24 | if(this.else_body!=null){ 25 | ln_elsbd++; 26 | this.else_body.genCode(codegen); 27 | } 28 | codegen.mp_label2line.put(lb_elsbd, ln_elsbd); 29 | codegen.replaceLb(lb_ifbd); 30 | //codegen.replaceLb(lb_elsbd); 31 | //codegen.labels_ifbd.remove(); 32 | //codegen.labels_elsbd.remove(); 33 | codegen.mp_label2line.remove(lb_ifbd); 34 | codegen.mp_label2line.remove(lb_elsbd); 35 | codegen.rps_code_list.remove(lb_ifbd); 36 | //codegen.rps_code_list.remove(lb_elsbd); 37 | return true; 38 | } 39 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 40 | if(this.else_body==null){ 41 | return this.if_body.genSymTb(codegen); 42 | } 43 | else{ 44 | return this.if_body.genSymTb(codegen)&&this.else_body.genSymTb(codegen); 45 | } 46 | } 47 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 48 | if(this.else_body==null){ 49 | return this.if_body.checkType(codegen); 50 | } 51 | else{ 52 | return this.if_body.checkType(codegen)&&this.else_body.checkType(codegen); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_Sg.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_Sg.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_Sg.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.AST; 4 | import Parser.CodeGenerator; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class Stmt_Sg extends AST { 10 | SgStmt_AsgnVar asn_var; 11 | SgStmt_DefVar def_var; 12 | SgStmt_CtrFlw ctrflw; 13 | Expr_Calc exp_calc; 14 | 15 | public boolean setSg(AST ast){ 16 | switch(this.getASTType()){ 17 | case "SgStmt_AsgnVar": 18 | this.asn_var=(SgStmt_AsgnVar)ast; 19 | break; 20 | case "SgStmt_DefVar": 21 | this.def_var=(SgStmt_DefVar)ast; 22 | break; 23 | case "SgStmt_CtrFlw": 24 | this.ctrflw=(SgStmt_CtrFlw)ast; 25 | break; 26 | case "Expr_Calc": 27 | this.exp_calc=(Expr_Calc)ast; 28 | break; 29 | default:break; 30 | } 31 | return true; 32 | } 33 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 34 | switch(this.getASTType()){ 35 | case "SgStmt_AsgnVar": 36 | this.asn_var.genCode(codegen); 37 | break; 38 | case "SgStmt_DefVar": 39 | this.def_var.genCode(codegen); 40 | break; 41 | case "SgStmt_CtrFlw": 42 | this.ctrflw.genCode(codegen); 43 | break; 44 | case "Expr_Calc": 45 | this.exp_calc.genCode(codegen); 46 | break; 47 | default:break; 48 | } 49 | return true; 50 | } 51 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 52 | switch(this.getASTType()){ 53 | case "SgStmt_AsgnVar": 54 | return this.asn_var.genSymTb(codegen); 55 | case "SgStmt_DefVar": 56 | return this.def_var.genSymTb(codegen); 57 | case "SgStmt_CtrFlw": 58 | return this.ctrflw.genSymTb(codegen); 59 | case "Expr_Calc": 60 | return this.exp_calc.genSymTb(codegen); 61 | default:return false; 62 | } 63 | } 64 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 65 | switch(this.getASTType()){ 66 | case "SgStmt_AsgnVar": 67 | return this.asn_var.checkType(codegen); 68 | case "SgStmt_DefVar": 69 | return this.def_var.checkType(codegen); 70 | case "SgStmt_CtrFlw": 71 | return this.ctrflw.checkType(codegen); 72 | case "Expr_Calc": 73 | return this.exp_calc.checkType(codegen); 74 | default:return false; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_Whl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/Stmt_Whl.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/Stmt_Whl.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.GenCodeException; 6 | import Parser.TypeSys.GenSymTblException; 7 | import Parser.TypeSys.TypeCheckException; 8 | 9 | public class Stmt_Whl extends AST { 10 | ExprCalc_Bool bool_exp; 11 | AST_StmtList stmt_list; 12 | 13 | 14 | public boolean setwhl(ExprCalc_Bool bool_exp,AST_StmtList stmt_list){ 15 | this.bool_exp=bool_exp; 16 | this.stmt_list=stmt_list; 17 | return true; 18 | } 19 | public boolean genCode(CodeGenerator codegen) throws GenCodeException { 20 | int ln_whlbg=codegen.getLineNo()+1; 21 | this.bool_exp.genCode(codegen); 22 | String lb_whlbg=":"+codegen.getTmpSn(); 23 | String lb_whlend=":"+codegen.getTmpSn(); 24 | IRCode code=new IRCode("while",this.bool_exp.getVal(),String.valueOf(codegen.getLineNo()+2),lb_whlend); 25 | codegen.addCode(code); 26 | //int ln_whlbd =codegen.getLineNo()+1; 27 | codegen.labels_whlbg.add(lb_whlbg); 28 | codegen.labels_whlend.add(lb_whlend); 29 | codegen.mp_label2line.put(lb_whlbg, ln_whlbg); 30 | codegen.getRpsLst(lb_whlbg).add(code); 31 | this.stmt_list.genCode(codegen); 32 | int ln_whlend =codegen.getLineNo()+2; 33 | codegen.mp_label2line.put(lb_whlend, ln_whlend); 34 | codegen.replaceLb(lb_whlbg); 35 | codegen.labels_whlbg.remove(); 36 | codegen.labels_whlend.remove(); 37 | codegen.mp_label2line.remove(lb_whlbg); 38 | codegen.mp_label2line.remove(lb_whlend); 39 | codegen.rps_code_list.remove(lb_whlbg); 40 | code=new IRCode("goto",String.valueOf(ln_whlbg),null,null); 41 | codegen.addCode(code); 42 | return true; 43 | } 44 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 45 | return this.bool_exp.genSymTb(codegen)&&this.stmt_list.genSymTb(codegen); 46 | } 47 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 48 | return this.bool_exp.checkType(codegen)&&this.stmt_list.checkType(codegen); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/TypeExp.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | 6 | public class TypeExp extends AST { 7 | TypeExp_Arr type_array; 8 | TypeExp_Bsc type_basic; 9 | TypeExp_Func type_func; 10 | TypeExp_Idn type_idn; 11 | TypeExp_Gnrc type_gnrc; 12 | String rst_type; 13 | //T_Type t_type; 14 | 15 | public boolean setTypeExp(AST ast){ 16 | switch(this.getASTType()){ 17 | case "TypeExp_Arr": 18 | this.type_array = (TypeExp_Arr)ast; 19 | break; 20 | case "TypeExp_Bsc": 21 | this.type_basic = (TypeExp_Bsc)ast; 22 | break; 23 | case "TypeExp_Func": 24 | this.type_func = (TypeExp_Func)ast; 25 | break; 26 | case "TypeExp_Idn": 27 | this.type_idn = (TypeExp_Idn)ast; 28 | break; 29 | case "TypeExp_Gnrc": 30 | this.type_gnrc = (TypeExp_Gnrc)ast; 31 | break; 32 | default: 33 | return false; 34 | } 35 | return true; 36 | } 37 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 38 | switch(this.getASTType()){ 39 | case "TypeExp_Arr": 40 | this.type_array.genCode(codegen); 41 | break; 42 | case "TypeExp_Bsc": 43 | this.type_basic.genCode(codegen); 44 | break; 45 | case "TypeExp_Func": 46 | this.type_func.genCode(codegen); 47 | break; 48 | case "TypeExp_Idn": 49 | this.type_idn.genCode(codegen); 50 | break; 51 | case "TypeExp_Gnrc": 52 | this.type_gnrc.genCode(codegen); 53 | break; 54 | default: 55 | return false; 56 | } 57 | return true; 58 | } 59 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 60 | boolean b; 61 | switch(this.getASTType()){ 62 | case "TypeExp_Arr": 63 | b=this.type_array.genSymTb(codegen); 64 | this.rst_type=this.type_array.rst_type; 65 | break; 66 | case "TypeExp_Bsc": 67 | b=this.type_basic.genSymTb(codegen); 68 | this.rst_type=this.type_basic.rst_type; 69 | break; 70 | case "TypeExp_Func": 71 | b=this.type_func.genSymTb(codegen); 72 | this.rst_type=this.type_func.rst_type; 73 | break; 74 | case "TypeExp_Idn": 75 | b=this.type_idn.genSymTb(codegen); 76 | this.rst_type=this.type_idn.rst_type; 77 | break; 78 | case "TypeExp_Gnrc": 79 | b=this.type_gnrc.genSymTb(codegen); 80 | this.rst_type=this.type_gnrc.rst_type; 81 | break; 82 | default: 83 | return false; 84 | } 85 | return b; 86 | } 87 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 88 | boolean b; 89 | switch(this.getASTType()){ 90 | case "TypeExp_Arr": 91 | b=this.type_array.checkType(codegen); 92 | this.rst_type=this.type_array.rst_type; 93 | break; 94 | case "TypeExp_Bsc": 95 | b=this.type_basic.checkType(codegen); 96 | this.rst_type=this.type_basic.rst_type; 97 | break; 98 | case "TypeExp_Func": 99 | b=this.type_func.checkType(codegen); 100 | this.rst_type=this.type_func.rst_type; 101 | break; 102 | case "TypeExp_Idn": 103 | b=this.type_idn.checkType(codegen); 104 | this.rst_type=this.type_idn.rst_type; 105 | break; 106 | case "TypeExp_Gnrc": 107 | b=this.type_gnrc.checkType(codegen); 108 | this.rst_type=this.type_gnrc.rst_type; 109 | break; 110 | default: 111 | return false; 112 | } 113 | return b; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Arr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/TypeExp_Arr.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Arr.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.IR.*; 5 | import Parser.TypeSys.*; 6 | 7 | public class TypeExp_Arr extends AST { 8 | TypeExp type_pre; 9 | String ele_type; 10 | String rst_type; 11 | int dim=1; 12 | 13 | public TypeExp getPreType() { 14 | return type_pre; 15 | } 16 | public void setPreType(TypeExp type_pre) { 17 | this.type_pre = type_pre; 18 | } 19 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 20 | //IRCode code=new IRCode("ArrType",this.rst_type,this.ele_type,String.valueOf(this.dim)); 21 | //codegen.addCode(code); 22 | return true; 23 | } 24 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 25 | if(this.type_pre!=null){ 26 | if(!this.type_pre.genSymTb(codegen))return false; 27 | if(this.type_pre.getASTType().equals("TypeExp_Arr")){ 28 | this.dim=this.type_pre.type_array.dim+1; 29 | } 30 | else return true; 31 | } 32 | return true; 33 | } 34 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 35 | if(this.type_pre!=null&&!this.type_pre.checkType(codegen))return false; 36 | if(this.type_pre.getASTType().equals("TypeExp_Arr")){ 37 | this.ele_type=this.type_pre.type_array.ele_type; 38 | }else{ 39 | this.ele_type=this.type_pre.rst_type; 40 | } 41 | this.rst_type=this.ele_type+"["+this.dim+"]"; 42 | T_Array t=new T_Array(); 43 | t.setDims(this.dim); 44 | t.setEleType(this.ele_type); 45 | codegen.putTypeInSymTb(this.rst_type, t); 46 | t.setKType(T_Type.KType.t_arr); 47 | t.setTypeSig(this.rst_type); 48 | return true; 49 | } 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Bsc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/TypeExp_Bsc.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Bsc.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | 6 | public class TypeExp_Bsc extends AST { 7 | String b_type; 8 | String rst_type; 9 | T_Type t_type; 10 | 11 | public void setTypeT(String b_type) { 12 | this.b_type =b_type; 13 | this.rst_type=this.b_type; 14 | } 15 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 16 | return true; 17 | } 18 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 19 | this.t_type=codegen.getTypeInSymTb(this.b_type); 20 | return true; 21 | } 22 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 23 | switch(this.b_type){ 24 | case "int": 25 | break; 26 | case "double": 27 | break; 28 | case "bool": 29 | break; 30 | case "string": 31 | break; 32 | case "char": 33 | break; 34 | default: 35 | return false; 36 | } 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Func.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/TypeExp_Func.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Func.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | 6 | public class TypeExp_Func extends AST { 7 | //String ret_type; 8 | T_Function t_type; 9 | boolean isFixed; 10 | String rst_type; 11 | String ref_type; 12 | 13 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 14 | 15 | return true; 16 | } 17 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 18 | this.rst_type="function"; 19 | return true; 20 | } 21 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 22 | 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Gnrc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ASTs/TypeExp_Gnrc.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ASTs/TypeExp_Gnrc.java: -------------------------------------------------------------------------------- 1 | package Parser.ASTs; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | import Parser.TypeSys.*; 7 | 8 | public class TypeExp_Gnrc extends AST { 9 | TypeExp_Idn idn_type; 10 | Gnrc_ArgLst g_args; 11 | String rst_type; 12 | //T_Type t_type; 13 | 14 | public boolean setGnrcType(TypeExp_Idn idn_type,Gnrc_ArgLst args){ 15 | this.g_args=args; 16 | this.idn_type=idn_type; 17 | return true; 18 | } 19 | public boolean genCode(CodeGenerator codegen)throws GenCodeException{ 20 | //IRCode code=new IRCode("GnrcType",this.rst_type,this.idn_type.rst_type,this.args.rst_val); 21 | //codegen.addCode(code); 22 | return true; 23 | } 24 | public boolean genSymTb(CodeGenerator codegen)throws GenSymTblException{ 25 | if(!this.idn_type.genSymTb(codegen)) 26 | return false; 27 | if(!this.g_args.genSymTb(codegen)) 28 | return false; 29 | return true; 30 | } 31 | public boolean checkType(CodeGenerator codegen)throws TypeCheckException{ 32 | if(!this.idn_type.checkType(codegen)) 33 | return false; 34 | if(!this.g_args.checkType(codegen)) 35 | return false; 36 | T_Type t=codegen.getTypeInSymTb(this.idn_type.rst_type);//TODO 37 | if(!t.isGnrc()) 38 | throw new TypeCheckException("type error: not gnrc "+this.idn_type.rst_type); 39 | if(t.getGnrcPars().size()!=this.g_args.types_name.size()) 40 | throw new TypeCheckException("type error: gnrc par size "+this.idn_type.rst_type); 41 | LinkedList pars=t.getGnrcPars(); 42 | LinkedList args=this.g_args.types_name; 43 | T_Generic t1=new T_Generic(); 44 | t1.setCoreType(this.idn_type.rst_type); 45 | for(int i=0;i symbols=new ArrayList(); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Script_1/src/Parser/IR/IRCode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/IR/IRCode.class -------------------------------------------------------------------------------- /Script_1/src/Parser/IR/IRCode.java: -------------------------------------------------------------------------------- 1 | package Parser.IR; 2 | 3 | public class IRCode { 4 | String opt; 5 | String opd1; 6 | String opd2; 7 | String opd3; 8 | 9 | 10 | 11 | public IRCode(){} 12 | public IRCode(String opt,String opd1,String opd2,String opd3){ 13 | this.opd1=opd1; 14 | this.opd2=opd2; 15 | this.opd3=opd3; 16 | this.opt=opt; 17 | } 18 | public String getOpt() { 19 | return opt; 20 | } 21 | public String getOpd1() { 22 | return opd1; 23 | } 24 | public String getOpd2() { 25 | return opd2; 26 | } 27 | public String getOpd3() { 28 | return opd3; 29 | } 30 | public void setOpt(String opt) { 31 | this.opt = opt; 32 | } 33 | public void setOpd1(String opd1) { 34 | this.opd1 = opd1; 35 | } 36 | public void setOpd2(String opd2) { 37 | this.opd2 = opd2; 38 | } 39 | public void setOpd3(String opd3) { 40 | this.opd3 = opd3; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Script_1/src/Parser/IR/IROprator.java: -------------------------------------------------------------------------------- 1 | package Parser.IR; 2 | 3 | public class IROprator { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Parser/PackageManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/PackageManager.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ParseState.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/ParseState.class -------------------------------------------------------------------------------- /Script_1/src/Parser/ParseState.java: -------------------------------------------------------------------------------- 1 | package Parser; 2 | 3 | import java.util.*; 4 | 5 | public class ParseState {//graph stack 6 | boolean isFixed=true; //fixed or ambiguous 7 | ParseState pre_state; 8 | ArrayList pre_states; //multi path 9 | Symbol symbol; 10 | ArrayList symbols; //corresponding to multi path 11 | int state_sn=-1; 12 | int out_count=0; 13 | int det_depth=0; 14 | boolean addLink(ParseState pre_state,Symbol symbol){ 15 | if(this.isFixed ){ 16 | if(this.pre_state==null){ 17 | this.pre_state=pre_state; 18 | this.symbol=symbol; 19 | }else{ 20 | if(this.symbol.name.equals(symbol.name) && this.pre_state==pre_state){//merge 21 | this.symbol.merge(symbol); 22 | return false; 23 | } 24 | this.isFixed=false; 25 | this.pre_states=new ArrayList(); 26 | this.symbols=new ArrayList(); 27 | this.pre_states.add(this.pre_state); 28 | this.symbols.add(this.symbol); 29 | this.pre_states.add(pre_state); 30 | this.symbols.add(symbol); 31 | } 32 | }else{ 33 | for(int i=0;i getPath(){ 13 | LinkedList symbs=new LinkedList(); 14 | List symbsR=new ArrayList(); 15 | symbs.add(crt_symbol); 16 | Path pre_t=pre_path; 17 | while(pre_t!=null&&pre_t.crt_symbol!=null){ 18 | symbs.addFirst(pre_t.crt_symbol); 19 | pre_t=pre_t.pre_path; 20 | } 21 | while(!symbs.isEmpty()){ 22 | symbsR.add(symbs.removeFirst()); 23 | } 24 | return symbsR; 25 | } 26 | public Path addSymbol(Symbol symb){//need refactor? 27 | /*if(this.crt_symbol==null){ 28 | this.crt_symbol=symb; 29 | return this; 30 | }*/ 31 | Path path_t=new Path(); 32 | 33 | path_t.pre_path=this; 34 | path_t.crt_symbol=symb; 35 | path_t.path_start=this.path_start; 36 | return path_t; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Script_1/src/Parser/Symbol.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/Symbol.class -------------------------------------------------------------------------------- /Script_1/src/Parser/Symbol.java: -------------------------------------------------------------------------------- 1 | package Parser; 2 | 3 | import java.util.*; 4 | 5 | public class Symbol {//symbol in graph stack correspond to reduce path or merged paths 6 | AST ast; 7 | boolean isEmpty=true; 8 | boolean isMerged=false; 9 | HashSet asts; 10 | int line; 11 | Path path; 12 | String name; 13 | public boolean addAST(AST ast){ 14 | if(isEmpty){ 15 | isEmpty=false; 16 | } 17 | if(isMerged){ 18 | if(this.asts.contains(ast)){ 19 | return false; 20 | } 21 | this.asts.add(ast); //do merge 22 | this.ast=ast; //update the direct ast 23 | this.ast.merged_asts=this.asts; 24 | System.out.println("merge ast "+name+"--------------------------------------------------------------------------------------"); 25 | }else{ 26 | if(this.ast==null){ 27 | this.ast=ast; 28 | }else{ 29 | if(this.ast==ast){ 30 | return false; 31 | } 32 | this.asts=new HashSet(); 33 | this.isMerged=true; 34 | this.asts.add(this.ast); //do merge 35 | this.ast.setMerged(); 36 | this.asts.add(ast); 37 | this.ast=ast; //refresh outer ast 38 | this.ast.merged_asts=this.asts; 39 | System.out.println("merge ast "+name+"--------------------------------------------------------------------------------------"); 40 | } 41 | } 42 | return true; 43 | } 44 | public Symbol(){} 45 | public Symbol(Symbol sym){ 46 | this.name=sym.name; 47 | this.ast=sym.ast; 48 | /*if(sym.asts!=null){ 49 | this.asts=new ArrayList(); 50 | this.asts.addAll(sym.asts); 51 | }*/ 52 | this.asts=sym.asts; 53 | this.line=sym.line; 54 | this.isMerged=sym.isMerged; 55 | this.isEmpty=sym.isEmpty; 56 | } 57 | public boolean merge(Symbol symbol){ 58 | if(this==symbol){ 59 | return false; 60 | } 61 | if(symbol.isMerged){ 62 | for(AST ast_t:symbol.asts){ 63 | addAST(ast_t);//do merge 64 | addAST(symbol.ast);//refresh outer ast 65 | } 66 | }else{ 67 | addAST(symbol.ast);//do merge 68 | } 69 | return true; 70 | } 71 | } -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/GenCodeException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/GenCodeException.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/GenCodeException.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class GenCodeException extends Exception { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = -5274749744190527262L; 9 | public GenCodeException(){} 10 | public GenCodeException(String e){super(e);} 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/GenSymTblException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/GenSymTblException.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/GenSymTblException.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class GenSymTblException extends Exception { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = 7897356825005553399L; 9 | 10 | public GenSymTblException(){} 11 | public GenSymTblException(String e){super(e);} 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/R_Function.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/R_Function.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/R_Package.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import java.util.*; 4 | 5 | public class R_Package { 6 | HashMap sub_pkgs; 7 | HashMap func_inPkg; 8 | HashMapvar_inPkg; 9 | HashMap type_inPkg; 10 | String pkg_name; 11 | 12 | public HashMap getSubPkgs() { 13 | return sub_pkgs; 14 | } 15 | public void addSubPkgs(String s, R_Package sub_pkg) { 16 | if(this.sub_pkgs==null) 17 | this.sub_pkgs=new HashMap(); 18 | this.sub_pkgs.put(s,sub_pkg); 19 | } 20 | public HashMap getFuncsInPkg() { 21 | return func_inPkg; 22 | } 23 | public void addFuncInPkg(String s, T_Function func) { 24 | if(this.func_inPkg==null) 25 | this.func_inPkg=new HashMap(); 26 | this.func_inPkg.put(s, func); 27 | } 28 | public HashMap getVarsInPkg() { 29 | return var_inPkg; 30 | } 31 | public void addVarInPkg(String s, R_Variable var) { 32 | if(this.var_inPkg==null) 33 | this.var_inPkg= new HashMap(); 34 | this.var_inPkg.put(s, var); 35 | } 36 | public HashMap getTypesInPkg() { 37 | return type_inPkg; 38 | } 39 | public void addTypeInPkg(String s, T_Type t) { 40 | if(this.type_inPkg==null) 41 | this.type_inPkg=new HashMap(); 42 | this.type_inPkg.put(s,t); 43 | } 44 | public String getPkgName() { 45 | return pkg_name; 46 | } 47 | public void setPkgName(String pkg_name) { 48 | this.pkg_name = pkg_name; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/R_Variable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/R_Variable.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/R_Variable.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import Parser.*; 4 | import Parser.TypeSys.*; 5 | import java.util.*; 6 | 7 | public class R_Variable { 8 | private boolean isFinal; 9 | private boolean isStatic; 10 | private boolean isDummy; 11 | private boolean isField; 12 | private T_Class inClass; 13 | public T_Class getInClass() { 14 | return inClass; 15 | } 16 | public void setInClass(T_Class inClass) { 17 | this.inClass = inClass; 18 | } 19 | private String var_type; 20 | private LinkedList rst_val=new LinkedList(); 21 | private String name; 22 | 23 | public String getVarName() { 24 | return name; 25 | } 26 | public void setVarName(String name) { 27 | this.name = name; 28 | } 29 | public LinkedList getRstVal(){ 30 | return this.rst_val; 31 | } 32 | public void addRstVal(String tmp_addr) { 33 | this.rst_val.add(tmp_addr); 34 | } 35 | public boolean isDummy() { 36 | return isDummy; 37 | } 38 | public void setDummy() { 39 | this.isDummy = true; 40 | } 41 | public boolean isStatic() { 42 | return isStatic; 43 | } 44 | public void setStatic(boolean isStatic) { 45 | this.isStatic = isStatic; 46 | } 47 | public boolean isFinal() { 48 | return isFinal; 49 | } 50 | public void setFinal(boolean isFinal) { 51 | this.isFinal = isFinal; 52 | } 53 | public boolean isField() { 54 | return isField; 55 | } 56 | public void setField(boolean isField) { 57 | this.isField = isField; 58 | } 59 | public String getVarType() { 60 | return var_type; 61 | } 62 | public void setVarType(String type_def) { 63 | this.var_type = type_def; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/S_Array.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class S_Array { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/S_Object.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class S_Object { 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/S_Reference.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class S_Reference { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/S_Value.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class S_Value { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Alias.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class T_Alias extends T_Type { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Array.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Array.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Array.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import Parser.CodeGenerator; 4 | 5 | public class T_Array extends T_Type { 6 | String ele_type; 7 | int dims; 8 | {this.setKType(KType.t_arr);} 9 | //public T_Array(){ 10 | // 11 | //} 12 | public String getEleType() { 13 | return ele_type; 14 | } 15 | public void setEleType(String ele_type) { 16 | this.ele_type = ele_type; 17 | } 18 | public int getDims() { 19 | return dims; 20 | } 21 | public void setDims(int dims) { 22 | this.dims = dims; 23 | } 24 | public void incDims(){ 25 | this.dims++; 26 | } 27 | public void genTypeSig(CodeGenerator codegen) { 28 | String s=codegen.getTypeInSymTb(this.ele_type).getTypeSig()+"["+this.dims+"]"; 29 | this.setTypeSig(s); 30 | } 31 | 32 | 33 | public boolean canCastFrom(CodeGenerator codegen,T_Type type2){ 34 | if(type2.getKType()!=this.getKType()) 35 | return false; 36 | if(((T_Array)type2).getDims()!=this.dims) 37 | return false; 38 | T_Type t1=codegen.getTypeInSymTb(this.ele_type); 39 | T_Type t2=codegen.getTypeInSymTb(((T_Array)type2).getEleType()); 40 | return t1.canCastFrom(codegen, t2); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_BasicType$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_BasicType$1.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_BasicType$en_BType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_BasicType$en_BType.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_BasicType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_BasicType.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_BasicType.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import Parser.CodeGenerator; 4 | 5 | public class T_BasicType extends T_Type { 6 | en_BType btype; 7 | {this.setKType(KType.t_bsc);} 8 | //public T_BasicType(){ 9 | // this.setKType(KType.t_bsc); 10 | //} 11 | public T_BasicType(String t){ 12 | //this.setKType(KType.t_bsc); 13 | this.setTypeName(t); 14 | switch(t){ 15 | case "int": 16 | this.btype=en_BType.t_int; 17 | break; 18 | case "double": 19 | this.btype=en_BType.t_double; 20 | break; 21 | case "char": 22 | this.btype=en_BType.t_char; 23 | break; 24 | case "string": 25 | this.btype=en_BType.t_string; 26 | break; 27 | case "bool": 28 | this.btype=en_BType.t_bool; 29 | break; 30 | default:break;//modify in future 31 | } 32 | setTypeB(this.btype); 33 | } 34 | public en_BType getTypeB() { 35 | return btype; 36 | } 37 | public void setTypeB(en_BType btype) { 38 | this.btype = btype; 39 | switch(btype){ 40 | case t_int: 41 | this.setTypeSig("int"); 42 | break; 43 | case t_double: 44 | this.setTypeSig("double"); 45 | break; 46 | case t_char: 47 | this.setTypeSig("char"); 48 | break; 49 | case t_string: 50 | this.setTypeSig("string"); 51 | break; 52 | case t_bool: 53 | this.setTypeSig("bool"); 54 | break; 55 | } 56 | } 57 | 58 | public boolean canCastFrom(CodeGenerator codegen,T_Type type2){ 59 | if(!(type2 instanceof T_BasicType)) 60 | return false; 61 | T_BasicType type3=(T_BasicType)type2; 62 | if(this.btype==type3.btype) 63 | return true; 64 | if(this.btype==en_BType.t_int){ 65 | if(type3.btype==en_BType.t_int||type3.btype==en_BType.t_double||type3.btype==en_BType.t_string){ 66 | return true; 67 | } 68 | }else if(this.btype==en_BType.t_double){ 69 | if(type3.btype==en_BType.t_double||type3.btype==en_BType.t_int||type3.btype==en_BType.t_string){ 70 | return true; 71 | } 72 | }else if(this.btype==en_BType.t_char){ 73 | if(type3.btype==en_BType.t_char||type3.btype==en_BType.t_int||type3.btype==en_BType.t_string){ 74 | return true; 75 | } 76 | }else if(this.btype==en_BType.t_bool){ 77 | if(type3.btype==en_BType.t_bool||type3.btype==en_BType.t_int||type3.btype==en_BType.t_string){ 78 | return true; 79 | } 80 | } 81 | return false; 82 | } 83 | public enum en_BType{t_int,t_double,t_string,t_char,t_bool;} 84 | } 85 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Class.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Class.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Function.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Function.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Function.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | import Parser.IR.*; 6 | 7 | public class T_Function extends T_Type { 8 | private String ret_type; 9 | private LinkedList par_types; 10 | //private boolean isDynamic; 11 | {this.setKType(KType.t_func);} 12 | public String getRetType() { 13 | return ret_type; 14 | } 15 | /* public T_Function(){ 16 | 17 | } 18 | public T_Function(String s){ 19 | if(s.equals("dynamic")){ 20 | this.setDynamic(true); 21 | } 22 | }*/ 23 | public void setRetType(String ret_type) { 24 | this.ret_type = ret_type; 25 | } 26 | public LinkedList getParTypes() { 27 | return par_types; 28 | } 29 | public void setParTypes(LinkedList par_types) { 30 | this.par_types = par_types; 31 | } 32 | /* public boolean isDynamic() { 33 | return isDynamic; 34 | } 35 | public void setDynamic(boolean isDynamic) { 36 | this.isDynamic = isDynamic; 37 | this.setTypeSig("!dynamic"); 38 | }*/ 39 | public String genFuncSig(CodeGenerator codegen) { 40 | /* if(isDynamic){ 41 | this.setTypeSig("!dynamic"); 42 | return this.getTypeSig(); 43 | }*/ 44 | String s=""; 45 | if(this.isGnrc()){ 46 | s+="<"; 47 | for(String g:this.getGnrcPars()){ 48 | s+=codegen.getTypeInSymTb(g).getTypeSig()+","; 49 | } 50 | s=s.substring(0,s.length()-1)+">"; 51 | } 52 | //s=codegen.getTypeInSymTb(this.ret_type).getTypeSig()+"("; 53 | s+=ret_type+"("; 54 | if(this.par_types!=null){ 55 | for(String name:this.par_types){ 56 | s+=codegen.getTypeInSymTb(name).getTypeSig()+","; 57 | } 58 | s=s.substring(0,s.length()-1); 59 | } 60 | s=s+")"; 61 | this.setTypeSig(s); 62 | return this.getTypeSig(); 63 | } 64 | public void genTypeSig(CodeGenerator codegen){ 65 | this.genFuncSig(codegen); 66 | } 67 | public boolean isEqType(T_Function t){//TODO 68 | if(this.getTypeSig().equals("!dynamic")||t.getTypeSig().equals("!dynamic")) 69 | return true; 70 | if(this.getTypeSig().equals(t.getTypeSig())) 71 | return true; 72 | else 73 | return false; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Generic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Generic.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Generic.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | 6 | public class T_Generic extends T_Type { 7 | private String core_type; 8 | LinkedHashMap type_args=new LinkedHashMap();//why linkedhashmap, link for order of gen-sig, map for sym-tbl 9 | 10 | {this.setKType(KType.t_gnrc);this.setGnrc(true);} 11 | public String getCoreType() { 12 | return core_type; 13 | } 14 | public void setCoreType(String core_type) { 15 | this.core_type = core_type; 16 | } 17 | public HashMap getTypeArgTb() { 18 | return type_args; 19 | } 20 | //public void setTypeArgTb(LinkedHashMap type_args) { 21 | // this.type_args = type_args; 22 | //} 23 | 24 | public boolean canCastFrom(CodeGenerator codegen,T_Type type2){ 25 | if(type2.getKType()!=KType.t_gnrc) 26 | return false; 27 | T_Type t1=codegen.getTypeInSymTb(this.core_type); 28 | T_Type t2=codegen.getTypeInSymTb(((T_Generic)type2).getCoreType()); 29 | if(!t1.canCastFrom(codegen, t2)) 30 | return false; 31 | if(this.type_args.size()!=((T_Generic)type2).getTypeArgTb().size()) 32 | return false; 33 | for(String s1:this.type_args.keySet()){ 34 | T_Type t3=codegen.getTypeInSymTb(this.type_args.get(s1)); 35 | T_Type t4=codegen.getTypeInSymTb(((T_Generic)type2).getTypeArgTb().get(s1)); 36 | if(!t3.canCastFrom(codegen, t4)) 37 | return false; 38 | } 39 | return true; 40 | } 41 | public void genTypeSig(CodeGenerator codegen) { 42 | String s=codegen.getTypeInSymTb(this.core_type).getTypeName()+"<"; 43 | for(String name:this.type_args.keySet()){ 44 | T_Type t=codegen.getTypeInSymTb(this.type_args.get(name)); 45 | /*if(!t.isDummy()) 46 | s+=name+":"+t.getTypeSig()+","; 47 | else 48 | s+=name+",";*/ 49 | s+=t.getTypeSig()+","; 50 | 51 | } 52 | s=s.substring(0, s.length()-1)+">"; 53 | this.setTypeSig(s); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Interface.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Interface.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Interface.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import java.util.*; 4 | 5 | import Parser.*; 6 | 7 | public class T_Interface extends T_Type { 8 | private LinkedList extd_types=new LinkedList(); 9 | private HashMap methods=new HashMap(); 10 | HashSet all_extd=new HashSet(); 11 | private String scope="global"; 12 | 13 | 14 | {this.setKType(KType.t_intf);} 15 | public HashMap getMethods() { 16 | return methods; 17 | } 18 | public void setMethods(HashMap methods) { 19 | this.methods = methods; 20 | } 21 | public LinkedList getExtdTypes() { 22 | return extd_types; 23 | } 24 | public void setExtdTypes(LinkedList extd_types) { 25 | this.extd_types = extd_types; 26 | } 27 | public HashSet getAllExtd(){ 28 | return all_extd; 29 | } 30 | public String getScope() { 31 | return scope; 32 | } 33 | public void setScope(String scope) { 34 | this.scope = scope; 35 | } 36 | public void genTypeSig(CodeGenerator codegen) { 37 | //String s=codegen.getTypeInSymTb(this.getTypeName()).getTypeSig(); 38 | //if(this.isGnrc()) 39 | // s=s+"<"+this.getGnrcPars().size()+">"; 40 | //this.setTypeSig(s); 41 | String s=""; 42 | if(this.isGnrc()){ 43 | s+="<"; 44 | for(String g:this.getGnrcPars()){ 45 | s+=codegen.getTypeInSymTb(g).getTypeSig()+","; 46 | } 47 | s=s.substring(0,s.length()-1)+">"; 48 | } 49 | s+=this.getTypeName(); 50 | this.setTypeSig(s); 51 | } 52 | 53 | public boolean canCastFrom(CodeGenerator codegen,T_Type type2){ 54 | if(this.all_extd.contains(type2.getTypeName())) 55 | return true; 56 | else{ 57 | if(type2.canCastFrom(codegen, this)) 58 | return true; 59 | } 60 | return false; 61 | } 62 | public boolean checkAllExtd(CodeGenerator codegen){ 63 | for(String s:this.extd_types){ 64 | T_Interface t = (T_Interface) codegen.getTypeInSymTb(s); 65 | for(String name:t.getAllExtd()){ 66 | if(this.all_extd.contains(t)) 67 | return false; 68 | this.all_extd.add(name); 69 | } 70 | } 71 | return true; 72 | } 73 | public boolean checkAllMthd(CodeGenerator codegen){//add all methods in this type 74 | for(String s:this.extd_types){ 75 | T_Interface t = (T_Interface) codegen.getTypeInSymTb(s); 76 | for(String name:t.methods.keySet()){ 77 | R_Function r=t.methods.get(name); 78 | if(!this.methods.containsKey(name)){ 79 | this.methods.put(name, r); 80 | }else{ 81 | R_Function r1=this.methods.get(name); 82 | if(r.isMulti()){ 83 | for(String ts:r.getMulti().keySet()){ 84 | if(!r1.isCntnNameType(r.getMulti().get(ts))){ 85 | r1.addFuncR(r.getMulti().get(ts)); 86 | } 87 | else return false; 88 | } 89 | }else{ 90 | if(!r1.isCntnNameType(r)) 91 | r1.addFuncR(r); 92 | else return false; 93 | } 94 | } 95 | } 96 | } 97 | return true; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Type$KType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Type$KType.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Type.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/T_Type.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/T_Type.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | import java.util.*; 4 | import Parser.*; 5 | 6 | public class T_Type { 7 | private KType k_type;//kind of type 8 | private String type_name; 9 | private String type_sig;//code types for hash, search, compare 10 | private boolean isGnrc;//has generic pars as a core type 11 | private LinkedList pars_Gnrc=new LinkedList(); 12 | //private T_Generic gnrc_wrap; 13 | private boolean isDummy=false; 14 | 15 | 16 | 17 | public boolean isEqType(T_Type t){ 18 | if(this.type_sig.equals("auto")||t.type_sig.equals("auto")) 19 | return true; 20 | if(this.k_type!=null&&!this.k_type.equals(t.k_type)) 21 | return false; 22 | if(this.getTypeSig().equals(t.getTypeSig())) 23 | return true; 24 | else 25 | return false; 26 | } 27 | 28 | //sets and gets 29 | public boolean isDummy() { 30 | return isDummy; 31 | } 32 | public void setDummy() { 33 | this.isDummy = true; 34 | this.k_type=KType.t_dummy; 35 | } 36 | public boolean isGnrc() { 37 | return isGnrc; 38 | } 39 | public void setGnrc(boolean hasGnrcPar) { 40 | this.isGnrc = hasGnrcPar; 41 | } 42 | public String getTypeName() { 43 | return type_name; 44 | } 45 | public void setTypeName(String type_name) { 46 | this.type_name = type_name; 47 | this.type_sig=this.type_name; 48 | } 49 | public String getTypeSig() { 50 | return type_sig; 51 | } 52 | public void setTypeSig(String type_code) { 53 | this.type_sig = type_code; 54 | } 55 | public void genTypeSig(CodeGenerator codegen){ 56 | 57 | } 58 | public boolean canAsnFrom(CodeGenerator codegen,T_Type type2){ //xx =type2 xx 59 | if(this.canCastFrom(codegen, type2)) 60 | return true; 61 | return false; 62 | } 63 | public boolean canCastFrom(CodeGenerator codegen,T_Type type2){ //(type1) type2 64 | if(this.isDummy&&type2.isDummy&&this.type_name.equals(type2.type_name)){ 65 | return true; 66 | } 67 | if(this.type_sig.equals(type2.type_sig)) 68 | return true; 69 | if(this.type_sig.equals("auto")||type2.type_sig.equals("auto")) 70 | return true; 71 | if(this.type_sig.equals("function")){ 72 | if(type2.type_sig.equals("function")||type2.getKType()==KType.t_func) 73 | return true; 74 | } 75 | return false; 76 | } 77 | public LinkedList getGnrcPars() { 78 | return pars_Gnrc; 79 | } 80 | public void setGnrcPars(LinkedList pars_Gnrc) { 81 | this.pars_Gnrc = pars_Gnrc; 82 | } 83 | public KType getKType() { 84 | return k_type; 85 | } 86 | public void setKType(KType k_type) { 87 | this.k_type = k_type; 88 | } 89 | public enum KType{t_bsc,t_arr,t_func,t_gnrc,t_cls,t_intf,t_dummy} 90 | } 91 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/TypeCheckException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/TypeCheckException.class -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/TypeCheckException.java: -------------------------------------------------------------------------------- 1 | package Parser.TypeSys; 2 | 3 | public class TypeCheckException extends Exception { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = 6453384060514627816L; 9 | TypeCheckException(){} 10 | public TypeCheckException(String e){ 11 | super(e); 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Script_1/src/Parser/TypeSys/TypeChecker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/Parser/TypeSys/TypeChecker.class -------------------------------------------------------------------------------- /Script_1/src/test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/src/test.class -------------------------------------------------------------------------------- /Script_1/src/test.java: -------------------------------------------------------------------------------- 1 | import Parser.*; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.*; 6 | 7 | public class test { 8 | 9 | public static void main(String[] args) { 10 | 11 | 12 | Parser parser=new Parser(); 13 | parser.analyzeGrm("out_grammar.txt"); //System.out.println("import out_grammar.txt"); 14 | parser.analyzeAST("grammar_AST.txt"); //System.out.println("import grammar_AST.txt"); 15 | parser.analyzeLex("out_lexAnalyzer.txt"); //System.out.println("import out_lexAnalyzer.txt"); 16 | System.out.println("parser ready"); 17 | //String file_name="testcode"+File.separator+"yftest"+File.separator+"test"+File.separator+"script1"; 18 | String file_name="testcode"+File.separator+"script1"; 19 | if(args.length>0){ 20 | if(args[0].equals("-c")) 21 | file_name=args[1]; 22 | } 23 | 24 | PackageManager pkgman=new PackageManager(); 25 | try { 26 | pkgman.compile(parser, file_name); 27 | } catch (Exception e) { 28 | e.printStackTrace(); 29 | } 30 | System.out.println("finish compiling "+file_name); 31 | //parser.input(file_name); 32 | //parser.parse(); System.out.println("finish parsing"); 33 | //parser.output("out_parser.txt"); 34 | //CodeGenerator codegen=new CodeGenerator(); 35 | //AST tree=parser.getAST(); 36 | //asts_cq.add(tree); 37 | //asts_cgmap.put(tree, codegen); 38 | 39 | 40 | //tree.genSymTb(codegen); 41 | //tree.checkType(codegen); 42 | //tree.genCode(codegen); 43 | //codegen.outputFile("out_IR.txt"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Script_1/src/test1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class test1 { 4 | 5 | public static void main(String[] args) { 6 | String s="a\\\\b"; 7 | //String s2=s.replace('\\', '-'); 8 | String[] ss=s.split("\\\\"); 9 | for(String s1:ss){ 10 | System.out.println(s1); 11 | } 12 | // TODO Auto-generated method stub 13 | /* File dir=new File("/home/luyunfei"); 14 | 15 | //if(!dir.exists()){ 16 | // System.out.println("no"); 17 | //if(dir.mkdirs()) 18 | // System.out.println("crt");; 19 | //} 20 | try { 21 | System.out.println(dir.getCanonicalPath()); 22 | System.out.println(dir.getAbsolutePath()); 23 | File[] ss=dir.listFiles(); 24 | for(File f:ss){ 25 | System.out.println(f.getCanonicalPath()); 26 | } 27 | } catch (IOException e) { 28 | // TODO Auto-generated catch block 29 | e.printStackTrace(); 30 | }*/ 31 | 32 | 33 | } 34 | 35 | 36 | } -------------------------------------------------------------------------------- /Script_1/testcode/bin/script1.yfc: -------------------------------------------------------------------------------- 1 | 89597046 2 | script1 3 | defClass A global 4 | Fields 1 5 | defField a:int 6 | Methods 1 7 | defMethod f int() 8 | retExp int a 9 | end 10 | newObj A %1 11 | mov A a %1 12 | end 13 | -------------------------------------------------------------------------------- /Script_1/testcode/bin/yftest/test/script1.yfc: -------------------------------------------------------------------------------- 1 | 89597046 2 | script1 3 | package yftest.test 4 | defClass functor global 5 | Fields 1 6 | defField t:T1 7 | Methods 1 8 | defMethod fmap functor(function,functor) 9 | defGnrcPar T2 10 | defFuncPar function f 11 | defFuncPar functor wa 12 | getField %1 wa t 13 | getFuncObj f 14 | pushFuncArg %1 15 | invoke T2 %2 16 | mov T2 b %2 17 | newObj functor %3 18 | mov functor fb %3 19 | getField %4 fb t 20 | mov T2 %4 b 21 | retExp functor fb 22 | end 23 | defClass Monad global 24 | extends 1 25 | functor 26 | Methods 2 27 | defMethod rt Monad(T1) 28 | defFuncPar T1 t 29 | newObj Monad %13 30 | mov Monad ma %13 31 | getField %14 ma t 32 | mov T1 %14 t 33 | retExp Monad ma 34 | end 35 | defMethod lk Monad(Monad,function) 36 | defGnrcPar T2 37 | defFuncPar Monad ma 38 | defFuncPar function f 39 | getField %15 ma t 40 | getFuncObj f 41 | pushFuncArg %15 42 | invoke Monad %16 43 | mov Monad mb %16 44 | retExp Monad mb 45 | end 46 | newObj functor %5 47 | mov functor a %5 48 | getField %6 a t 49 | load_i %7 1 50 | mov int %6 %7 51 | newObj functor %8 52 | mov functor b %8 53 | defLambdaExp #9 bool(int) 54 | defFuncPar int i 55 | load_i %11 0 56 | GT %10 i %11 57 | if %10 13 15 58 | retExp bool true 59 | goto 16 60 | retExp bool false 61 | end 62 | mov function f #9 63 | getMethod fmap functor(function,functor) a 64 | pushTypeArg bool 65 | pushFuncArg f 66 | pushFuncArg a 67 | invoke functor %12 68 | mov functor b %12 69 | end 70 | -------------------------------------------------------------------------------- /Script_1/testcode/script1.yfl: -------------------------------------------------------------------------------- 1 | class A{ 2 | int a; 3 | int f(){ 4 | return a; 5 | } 6 | } 7 | A a=new A(); -------------------------------------------------------------------------------- /Script_1/testcode/yftest/test/script1.yfl: -------------------------------------------------------------------------------- 1 | 2 | package yftest.test; 3 | /* 4 | int fact(int a){ 5 | if(a==1) return 1; 6 | else 7 | return a*fact(a-1); 8 | } 9 | int fib(int a){ 10 | if(a<3) return 1; 11 | else 12 | return fib(a-1)+fib(a-2); 13 | } 14 | int a=1; 15 | int b=fib(a); 16 | 17 | int[][] as=new int[10][10]; 18 | as[0][0]=6; 19 | int b=as[1][1]; 20 | 21 | class A{ 22 | T1 a; 23 | T1 f(T2 b,T3 c){return this.a;} 24 | } 25 | 26 | class B extends A{ 27 | T1 b; 28 | T1 f(T2 b,T3 c){return this.b;} 29 | } 30 | 31 | A ga=new B(); 32 | bool t=ga.f(1,2); 33 | 34 | function f = ( int a ) -> { return ()->{return 1;}; }; 35 | f(1); 36 | */ 37 | 38 | class functor { 39 | T1 t; 40 | functor fmap (function f, functor wa) { 41 | T2 b=f(wa.t); 42 | functor fb=new functor(); 43 | fb.t=b; 44 | return fb; 45 | } 46 | } 47 | 48 | functor a=new functor(); 49 | a.t=1; 50 | functor b=new functor(); 51 | function f=(int i)->{if(i>0)return true; else return false;}; 52 | b=a.fmap( f , a); 53 | 54 | 55 | class Monad extends functor{ 56 | Monad rt(T1 t){ 57 | Monad ma=new Monad(); 58 | ma.t=t; 59 | return ma; 60 | } 61 | Monad lk(Monad ma, function f){ 62 | Monad mb=f(ma.t); 63 | return mb; 64 | } 65 | } -------------------------------------------------------------------------------- /Script_1/tokens.txt: -------------------------------------------------------------------------------- 1 | //tokens 2 | int_num [0-9]+ 3 | double_num [0-9]+.[0-9]+ 4 | identifier _*[a-zA-Z]+[a-zA-Z0-9_]* 5 | 6 | //reserved 7 | if if 8 | else else 9 | while while 10 | do do 11 | for for 12 | return return 13 | break break 14 | continue continue 15 | goto goto 16 | int int 17 | long long 18 | float float 19 | double double 20 | bool bool 21 | void void 22 | true true 23 | false false 24 | string string 25 | char char 26 | class class 27 | interface interface 28 | extends extends 29 | implements implements 30 | data data 31 | deriving deriving 32 | type type 33 | typedef typedef 34 | typename typename 35 | newtype newtype 36 | lambda lambda 37 | public public 38 | private private 39 | static static 40 | new new 41 | final final 42 | finally finally 43 | finalize finalize 44 | try try 45 | catch catch 46 | this this 47 | super super 48 | main main 49 | function function 50 | import import 51 | package package 52 | 53 | //operator 54 | || || 55 | && && 56 | ! ! 57 | > > 58 | >= >= 59 | < < 60 | <= <= 61 | == == 62 | != != 63 | + + 64 | - - 65 | ++ ++ 66 | -- -- 67 | * * 68 | / / 69 | += += 70 | -= -= 71 | *= *= 72 | /= /= 73 | = = 74 | -> -> 75 | => => 76 | <- <- 77 | \ \ 78 | _ _ 79 | : : 80 | @ @ 81 | $ $ 82 | ? ? 83 | ( ( 84 | ) ) 85 | { { 86 | } } 87 | [ [ 88 | ] ] 89 | , , 90 | . . 91 | ; ; 92 | eof eof 93 | 94 | //end 95 | -------------------------------------------------------------------------------- /Script_1/yflang.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/Script_1/yflang.jar -------------------------------------------------------------------------------- /SimpleCalc_1 with LL manual parser.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nklofy/Compiler/99f3e8affd6ea0e7266d661464c98c190daa3821/SimpleCalc_1 with LL manual parser.zip --------------------------------------------------------------------------------