This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
java.stg
150 lines (119 loc) · 3.07 KB
/
java.stg
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
// Simple Translation Dictionaries
booleanValue ::= [
"true" : "true",
"false" : "false",
default : "null"
]
typeValue ::= [
"Integer" : "int",
"Real" : "double",
"Boolean" : "boolean",
"String" : "String",
default : "null"
]
typeFormat ::= [
"Integer" : "",
"Real" : "",
"Boolean" : "",
"String" : "",
default : "null"
]
operators ::= [
"*" : "*",
"/" : "/",
"%" : "%",
"+" : "+",
"-" : "-",
"==" : "==",
"!=" : "!=",
"<" : "<",
">" : ">",
"<=" : "<=",
">=" : ">=",
"not": "!",
"or" : "||",
"and": "&&",
default : "null"
]
powerTypeCast ::= [
"Integer" : "(int)",
"Real" : "(double)",
default : "null"
]
// Actual String Templates
//Generate Class
module(name,stat) ::= <<
import java.util.Scanner;
public class <name> {
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
<stats(stat)>
}
}
>>
stats(stat) ::= <<
<if(stat)><stat; separator="\n"><endif>
>>
//Declare New Variable (and assign it a value if need-be)
init(value) ::= "<if(value)> = <value><endif>" //Check if there's a value
declaration(type,var,value) ::= "<typeValue.(type)> <var><init(value)>;"
//Assign Value to Variable
assign(stat,var,value) ::= <<
<stats(stat)>
<var> = <value>;
>>
//Print expression
initUnit(unit) ::= "<if(unit)> + \"<unit>\"<endif>" //Check if there's a unit
print(stat,expr,unit) ::= <<
<stats(stat)>
System.out.println(<expr><initUnit(unit)>);
>>
//Input from user
input(stat, prompt,var) ::= <<
<stats(stat)>
System.out.print(<prompt>);
String <var> = userInput.nextLine();
>>
//Expression (will always be binary)
expression(type,var,e1,op,e2) ::= "<declaration(type,var,[e1,\" \",operators.(op),\" \",e2])>"
//Power expression
powerExpr(type,var,e1,e2) ::= "<declaration(type,var,[powerTypeCast.(type),\"Math.pow(\", e1, \", \", e2, \")\"])>"
//Expression Equals
expressionEquals(type,var,e1,e2) ::= "<declaration(type,var,[e1,\".equals(\",e2,\")\"])>"
//Expression !Equals
expressionNotEquals(type,var,e1,e2) ::= "<declaration(type,var,[\"!\",e1,\".equals(\",e2,\")\"])>"
//Expression !Equals
expressionNot(type,var,e1) ::= "<declaration(type,var,[\"!\",e1])>"
//Conditional IF-ELSE
conditional(stat,var,trueStats,falseStats) ::= <<
<stats(stat)>
if (<var>) {
<trueStats>
}<if(falseStats)>
else {
<falseStats>
}<endif>
>>
//Conditional WHILE
conditionalWhile(stat,var,checkCondition,trueStats) ::= <<
<stats(stat)>
while (<var>) {
<trueStats>
<checkCondition> //Recheck Conditions
}
>>
//Conditional FOR
conditionalFor(stat,var,cond,updateCond,updateIncr,inc,trueStats) ::= <<
<stats(stat)>
for(<var><cond>;<inc>){
<updateCond> //Recheck Break Condition
if(!<cond>){
break;
}
<trueStats>
<updateIncr> //Update Increment variable
}
>>
// binaryOperator ('+' | '-')
binaryOperator(type,var,e1,operator) ::= "<typeValue.(type)> <var><if(e1)> = <operator>(<e1>)<endif>;"
absoluteValue(type,var,e1) ::= "<typeValue.(type)> <var><if(e1)> = Math.abs(<e1>)<endif>;"