-
Notifications
You must be signed in to change notification settings - Fork 4
/
Language.groovy
168 lines (137 loc) · 7 KB
/
Language.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cn.wenyan.compiler.script.libs
import cn.wenyan.compiler.CompileBackend
import cn.wenyan.compiler.GroovyCompiler
import cn.wenyan.compiler.LanguageCompiler
import cn.wenyan.compiler.WenyanToGroovyBackend
import cn.wenyan.compiler.lib.ArrayUtils
import cn.wenyan.compiler.lib.MathUtil
import cn.wenyan.compiler.utils.GroovyPrettyCode
import cn.wenyan.compiler.utils.PrettyCode
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
@CompileStatic
class Language {
public static final Language GROOVY = new Language([
(Syntax.VAR_DEFINE) : "def $NAME = $VALUE",
(Syntax.FOR_NUMBER) : "for(_ans$INDEX in 1..$RANGE){",
(Syntax.COMMENT) : "/*$COMMENT*/",
(Syntax.FOR_EACH) : "for($ELEMENT in $ARRAY){",
(Syntax.FOR_END) : "}",
(Syntax.IF) : "if($BOOL){",
(Syntax.IF_END) : "}",
(Syntax.IF_BREAK) : "if($BOOL)break",
(Syntax.WHILE_TRUE) : "while(true){",
(Syntax.ELSE) : "}else{",
(Syntax.BREAK) : "break",
(Syntax.RETURN) : "return $VALUE",
(Syntax.FUNCTION_END) : "}",
(Syntax.DEFINE_FUNCTION) : "def $NAME($ARGS){",//
(Syntax.FUNCTION_ARGS_SPLIT) : ",",
(Syntax.IMPORT) : "import $LIB",
(Syntax.IMPORT_STATIC) : "import static $LIB.$METHOD",
(Syntax.IMPORT_STATIC_SEPARATE): "true",
(Syntax.IMPORT_SPLIT) : "null",
(Syntax.MATH_ADD) : "$NAME+$VALUE",
(Syntax.MATH_LESS) : "$NAME-$VALUE",
(Syntax.MATH_MULTI) : "$NAME*$VALUE",
(Syntax.MATH_EXCEPT) : "$NAME/$VALUE",
(Syntax.MATH_REMAIN) : "mod($NAME,$VALUE)",
(Syntax.BIGGER) : ">",
(Syntax.SMALLER) : "<",
(Syntax.EQUAL) : "==",
(Syntax.AND) : "&&",
(Syntax.OR) : "||",
(Syntax.PRINT) : "println($VALUE .toString())",
(Syntax.NEGATE) : "!",
(Syntax.CHANGE) : "$NAME = $VALUE",
(Syntax.REPLACE_ARRAY) : "$NAME[getIndex($INDEX)] = $VALUE",
(Syntax.STRING) : "\"\"\"",
(Syntax.ARRAY_ADD) : NAME+".add($VALUE)",
(Syntax.INNER_FUNCTION) : "def $NAME \n $NAME = {\n $ARGS->",
(Syntax.INNER_FUNCTION_NO_ARGS): "def $NAME \n $NAME = {\n ",
(Syntax.TRUE) : "true",
(Syntax.FALSE) : "false",
(Syntax.NOT_BIG_THAN) : "<=",
(Syntax.NOT_SMALL_THAN) : ">=",
(Syntax.NEGATE_EQUAL) : "!=",
(Syntax.SLICE) : "ArrayUtils.slice(getArray($NAME))",
(Syntax.SIZE) : "ArrayUtils.length(getArray($NAME))",
(Syntax.RUN_FUNCTION) : "def $VALUE = $NAME($ARGS)",
(Syntax.OBJECT_INNER) : ".",
(Syntax.NUMBER_SUGAR) : "$NAME",
(Syntax.STRING_APPEND) : "+",
(Syntax.ARRAY_GET) : "getArray($NAME)[getIndex($INDEX)]",
(Syntax.INT_TYPE) : "BigDecimal",
(Syntax.STRING_TYPE) : "String",
(Syntax.ARRAY_TYPE) : "JSArray",
(Syntax.BOOL_TYPE) : "boolean",
(Syntax.VAR_TYPE) : "def",
(Syntax.DOUBLE_TYPE) : "double",
(Syntax.FUNCTION_ARG_DEFINE) : "$TYPE $NAME",
(Syntax.NULL) : "null",
(Syntax.DEFINE_ARRAY) : "new JSArray()",
(Syntax.DEFINE_INT) : "0",
(Syntax.DEFINE_STRING) : "''",
(Syntax.IMPORT_WITH) : ("import cn.wenyan.compiler.lib.*\nimport static "+ArrayUtils.name+".getArray\nimport static "+ArrayUtils.name+".getIndex\nimport static "+ MathUtil.name+".mod"),
(Syntax.NOT) : "!",
(Syntax.REQUIRE_SCRIPT) : "",
(Syntax.CONTINUE) : "continue",
(Syntax.ELSE_IF) : "}else ",
(Syntax.CONCAT) : (NAME+".putAll($VALUE)"),
(Syntax.TRY) : "try{",
(Syntax.THROW) : ("Exception $NAME = new Exception($EXCEPTION)\nthrow $NAME"),
(Syntax.CATCH) : "}catch($NAME){",
(Syntax.EXCEPTION_IF) : (NAME+".message.equals($EXCEPTION)"),
(Syntax.CATCH_END) : "}",
(Syntax.SHELL_VAR) : "$NAME = $VALUE",
(Syntax.DEFINE_OBJECT) : "[:]",
(Syntax.DELETE) : (NAME+".remove($INDEX)"),
(Syntax.OBJECT_TYPE) : ("Map "),
(Syntax.DEFINE) : "def $NAME",
(Syntax.GIVE_FUNCTION) : "$NAME = {\n $ARGS->",
(Syntax.DEFINE_GIVE_FUNCTION) : "def $NAME = {\n $ARGS->"
],new GroovyCompiler(),new GroovyPrettyCode(),new WenyanToGroovyBackend())
static final String EXCEPTION ="%{exception}%"
static final String TYPE = "%{type}%"
static final String NAME = "%{name}%"
static final String VALUE = "%{value}%"
static final String INDEX = "%{index}%"
static final String RANGE = "%{range}%"
static final String COMMENT = "%{comment}%"
static final String ELEMENT = "%{element}%"
static final String ARRAY = "%{array}%"
static final String BOOL = "%{bool}%"
static final String ARGS = "%{args}%"
static final String LIB = "%{lib}%"
static final String METHOD = "%{method}%"
protected Map<Syntax,String> syntaxLib
protected LanguageCompiler compiler
protected PrettyCode pretty
protected CompileBackend backend
static Map<String,Language> languageMap = [:]
Language(Map syntaxLib,LanguageCompiler compiler,PrettyCode pretty,CompileBackend backend){
this.syntaxLib = syntaxLib
this.compiler = compiler
this.pretty = pretty
this.backend = backend
}
String getSyntax(Syntax property){
return syntaxLib[property]
}
LanguageCompiler languageCompiler(){
return compiler
}
PrettyCode getPretty() {
return pretty
}
CompileBackend getCompileBackend(){
return backend
}
@CompileStatic(TypeCheckingMode.SKIP)
static void addLanguage(String name,Map syntaxLib,LanguageCompiler compiler,PrettyCode pretty,CompileBackend backend){
languageMap.put(name,new Language(syntaxLib,compiler,pretty,backend))
}
static Language getLanguage(String name){
return languageMap.get(name)
}
}