Skip to content

Commit d3c533d

Browse files
authored
Merge pull request #3 from TTimeLanguage/isac
final merge
2 parents 7513e49 + 67da6ef commit d3c533d

40 files changed

+543
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ TTime_Compiler.iml
6262
doc/*
6363
out/*
6464
*.iml
65+
/bin/

src/CodeGenerator/CodeGenerator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import Syntax.Expression;
77
import Syntax.Operator;
88
import Syntax.Program;
9-
import Syntax.Type;
109

1110
import java.io.File;
1211
import java.io.FileWriter;
1312
import java.io.IOException;
14-
import java.util.*;
15-
import java.util.concurrent.TimeUnit;
13+
import java.util.ArrayList;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.LinkedHashMap;
1617

1718

1819
public class CodeGenerator {

src/CodeGenerator/DefinedFunction.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Semantic.FunctionSet;
55
import Syntax.FunctionDeclaration;
66
import Syntax.ParamDeclaration;
7-
import Syntax.Statements;
87
import Syntax.Type;
98

109
import java.lang.reflect.InvocationTargetException;
@@ -29,9 +28,9 @@ public class DefinedFunction {
2928

3029
private static final String[] customFunc = {
3130
"getHour", "getMin", "getSec", "getYear", "getMonth", "getDay",
32-
"addTime", "subTime", "mulTime", "divTime", "modTime",
31+
"addTime", "subTime", "mulTime", "divTime", "modTime", "secToTime", "timeToSec",
3332
"addDate", "subDate",
34-
"validTime", "daysToDate"
33+
"validTime", "daysToDate", "makeTime"
3534
};
3635

3736
public static final HashSet<String> predefinedFunc = new HashSet<>(Arrays.asList(customFunc));
@@ -61,7 +60,9 @@ public static void defineFunc(FunctionSet functionSet, HashMap<String, ArrayList
6160
createFunc(Type.TIME, "mulTime", timeParam, intParam);
6261
createFunc(Type.TIME, "divTime", timeParam, intParam);
6362
createFunc(Type.TIME, "modTime", timeParam, intParam);
63+
createFunc(Type.TIME, "makeTime", intParam, intParam, intParam);
6464
createFunc(Type.TIME, "validTime", timeParam);
65+
createFunc(Type.TIME, "secToTime", intParam);
6566

6667
createFunc(Type.DATE, "addDate", dateParam, dateParam);
6768
createFunc(Type.DATE, "subDate", dateParam, dateParam);
@@ -234,6 +235,46 @@ protected static void modTime() {
234235
CodeGenerator.genCode("end");
235236
}
236237

238+
protected static void secToTime() {
239+
CodeGenerator.genFunc("secToTime", 1, 2, 2);
240+
CodeGenerator.genCode("sym", 2, 1, 1);
241+
242+
CodeGenerator.genCode("lod", 2, 1);
243+
244+
CodeGenerator.genCode("retv");
245+
CodeGenerator.genCode("end");
246+
}
247+
248+
protected static void timeToSec() {
249+
CodeGenerator.genFunc("timeToSec", 1, 2, 2);
250+
CodeGenerator.genCode("sym", 2, 1, 1);
251+
252+
CodeGenerator.genCode("lod", 2, 1);
253+
254+
CodeGenerator.genCode("retv");
255+
CodeGenerator.genCode("end");
256+
}
257+
258+
protected static void makeTime() {
259+
CodeGenerator.genFunc("makeTime", 3, 2, 2);
260+
CodeGenerator.genCode("sym", 2, 1, 1);
261+
CodeGenerator.genCode("sym", 2, 2, 1);
262+
CodeGenerator.genCode("sym", 2, 3, 1);
263+
264+
CodeGenerator.genCode("lod", 2, 1);
265+
CodeGenerator.genCode("ldc", 3600);
266+
CodeGenerator.genCode("mult");
267+
CodeGenerator.genCode("lod", 2, 2);
268+
CodeGenerator.genCode("ldc", 60);
269+
CodeGenerator.genCode("mult");
270+
CodeGenerator.genCode("lod", 2, 3);
271+
CodeGenerator.genCode("add");
272+
CodeGenerator.genCode("add");
273+
274+
CodeGenerator.genCode("retv");
275+
CodeGenerator.genCode("end");
276+
}
277+
237278
protected static void validTime() {
238279
CodeGenerator.genFunc("validTime", 1, 2, 2);
239280
CodeGenerator.genCode("sym", 2, 1, 1);

src/Lexer/Lexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private char nextChar() {
6262
public Token next() {
6363
do {
6464
if (isLetter(ch)) { // ident or keyword
65-
String spelling = concat(letters + digits);
65+
String spelling = concat(letters + digits + "_");
6666
return Token.keyword(spelling);
6767

6868
} else if (isDigit(ch)) { // int or float literal

src/Parser/Parser.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ private Program program() {
4747
Type t = type();
4848

4949
if (t.equals(Type.INT) && isMain()) {
50-
match(TokenType.Main); // int main
51-
match(TokenType.LeftParen); // (
52-
match(TokenType.RightParen); // )
53-
s = statements(); // Statements
50+
match(TokenType.Main); // int main
51+
match(TokenType.LeftParen); // (
52+
match(TokenType.RightParen); // )
53+
s = statements(); // Statements
5454
} else {
5555
String id = match(TokenType.Identifier);
5656

@@ -215,31 +215,31 @@ private Statement statement() {
215215
match(TokenType.Semicolon);
216216
return new Skip();
217217

218-
} else if (isIf()) { //if
218+
} else if (isIf()) { //if
219219
return IfStatement();
220220

221-
} else if (isLeftBrace()) { //block
221+
} else if (isLeftBrace()) { //block
222222
return block();
223223

224-
} else if (token.type().equals(TokenType.While)) { // while
224+
} else if (token.type().equals(TokenType.While)) { // while
225225
return WhileStatement();
226226

227-
} else if (token.type().equals(TokenType.Switch)) { // switch
227+
} else if (token.type().equals(TokenType.Switch)) { // switch
228228
return SwitchStatement();
229229

230-
} else if (token.type().equals(TokenType.For)) { // for
230+
} else if (token.type().equals(TokenType.For)) { // for
231231
return ForStatement();
232232

233-
} else if (token.type().equals(TokenType.Return)) { // return
233+
} else if (token.type().equals(TokenType.Return)) { // return
234234
return Return();
235235

236-
} else if (token.type().equals(TokenType.Break)) { // break
236+
} else if (token.type().equals(TokenType.Break)) { // break
237237
return Break();
238238

239-
} else if (token.type().equals(TokenType.Continue)) { // continue
239+
} else if (token.type().equals(TokenType.Continue)) { // continue
240240
return Continue();
241241

242-
} else { // expression
242+
} else { // expression
243243
return expression();
244244
}
245245
}

src/Syntax/ArrayInit.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ void display(int lev) {
6060
}
6161
}
6262

63+
/**
64+
* void type의 변수가 선언되었는지 확인
65+
* <p>
66+
* 배열의 크기가 양수인지 확인
67+
* <p>
68+
* 초기화가 되어있는 배열이라면 해당 초기화 식을 확인
69+
* <p>
70+
* 배열 크기와 초기화 식의 수를 확인, 초기화 식을 확인하고 type을 확인
71+
*/
6372
@Override
6473
protected void V(HashMap<String, Init> declarationMap) {
6574
// todo 확인

src/Syntax/ArrayRef.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ void display(int lev) {
3636
index.display(lev + 1);
3737
}
3838

39+
/**
40+
* 선언이 되어있는 변수의 이름인지 확인
41+
* <p>
42+
* <tt>ArrayInit</tt>의 객체인지 확인
43+
* <p>
44+
* 참조하려는 배열의 index가 Int인지 확인
45+
*/
3946
@Override
4047
protected void V(HashMap<String, Init> declarationMap) {
4148
// todo 확인 인덱스 범위 확인

src/Syntax/Binary.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ void display(int lev) {
4141
term2.display(lev + 1);
4242
}
4343

44+
/**
45+
* 좌변, 우변의 타당성 검사
46+
* <p>
47+
* 연산자가 = 이라면 좌변이 변수참조인지 확인
48+
* <p>
49+
* 양 변이 void type인지 확인
50+
* <p>
51+
* 변수에 type에 맞게 해당 연산자를 맞게 불러오도록 함
52+
*/
4453
@Override
4554
protected void V(HashMap<String, Init> declarationMap) {
4655
if (valid) return;
@@ -80,7 +89,7 @@ protected void V(HashMap<String, Init> declarationMap) {
8089
case Operator.TIME_GE:
8190
case Operator.TIME_ASSIGN:
8291
check(type2.equals(Type.TIME),
83-
type1 + " value can not have " + op.value + " operator with " + type2 + "type term");
92+
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
8493
op = operator;
8594
break;
8695

@@ -91,7 +100,7 @@ protected void V(HashMap<String, Init> declarationMap) {
91100
case Operator.TIME_DIV_ASSIGN:
92101
case Operator.TIME_MOD_ASSIGN:
93102
check(type2.equals(Type.INT),
94-
type1 + " value can not have " + op.value + " operator with " + type2 + "type term");
103+
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
95104
op = operator;
96105
break;
97106

@@ -148,7 +157,9 @@ protected void V(HashMap<String, Init> declarationMap) {
148157
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
149158

150159
if (type2.equals(Type.FLOAT)) {
151-
term1 = new TypeCast(Type.FLOAT, term1);
160+
TypeCast cast = new TypeCast(Type.FLOAT, term1);
161+
cast.expressionType = Type.FLOAT;
162+
term1 = cast;
152163
operator = Operator.floatMap(op.value);
153164
}
154165
op = operator;
@@ -164,7 +175,9 @@ protected void V(HashMap<String, Init> declarationMap) {
164175
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
165176

166177
if (type2.equals(Type.FLOAT)) {
167-
term2 = new TypeCast(Type.INT, term2);
178+
TypeCast cast = new TypeCast(Type.INT, term2);
179+
cast.expressionType = Type.INT;
180+
term2 = cast;
168181
}
169182
op = operator;
170183
break;
@@ -176,7 +189,9 @@ protected void V(HashMap<String, Init> declarationMap) {
176189
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
177190

178191
if (type2.equals(Type.FLOAT)) {
179-
term1 = new TypeCast(Type.FLOAT, term1);
192+
TypeCast cast = new TypeCast(Type.FLOAT, term1);
193+
cast.expressionType = Type.FLOAT;
194+
term1 = cast;
180195
operator = Operator.floatMap(op.value);
181196

182197
} else if (type2.equals(Type.TIME)) {
@@ -237,7 +252,9 @@ protected void V(HashMap<String, Init> declarationMap) {
237252
type1 + " value can not have " + op.value + " operator with " + type2 + " type term");
238253

239254
if (type2.equals(Type.INT)) {
240-
term2 = new TypeCast(Type.FLOAT, term2);
255+
TypeCast cast = new TypeCast(Type.FLOAT, term2);
256+
cast.expressionType = Type.FLOAT;
257+
term2 = cast;
241258
operator = Operator.floatMap(op.value);
242259
}
243260
op = operator;
@@ -573,7 +590,7 @@ public void genCode() {
573590
check(false, "Compiler error. never reach here. Binary genCode()");
574591
}
575592
break;
576-
593+
577594

578595
}
579596
}

src/Syntax/Block.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ void display(int lev) {
3232
}
3333
}
3434

35+
/**
36+
* block내의 statement를 확인
37+
*/
3538
@Override
3639
protected void V(HashMap<String, Init> declarationMap, Type functionType) {
3740
if (valid) return;
@@ -43,6 +46,9 @@ protected void V(HashMap<String, Init> declarationMap, Type functionType) {
4346
valid = true;
4447
}
4548

49+
/**
50+
* block내의 반복문이 포함된 statement를 확인
51+
*/
4652
@Override
4753
protected void V(HashMap<String, Init> declarationMap, Statement loopStatement) {
4854
if (valid) return;

src/Syntax/Break.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ void display(int lev) {
2222
System.out.println("Break");
2323
}
2424

25+
/**
26+
* 반복문이 없는 statement에서 break가 나오면 에러
27+
*/
2528
@Override
2629
protected void V(HashMap<String, Init> declarationMap, Type functionType) {
2730
check(false, "wrong statement. break keyword is not to be here");
2831
}
2932

33+
/**
34+
* while, for, switch문에서 break가 쓰이는지 확인
35+
*/
3036
@Override
3137
protected void V(HashMap<String, Init> declarationMap, Statement loopStatement, Type functionType) {
3238
check(loopStatement instanceof WhileStatement
@@ -41,6 +47,11 @@ protected void V(HashMap<String, Init> declarationMap, Statement loopStatement,
4147
public void genCode() {
4248
// todo
4349

44-
CodeGenerator.ujp(CodeGenerator.getLoopEndBranch(parentStatement.branchNum));
50+
if (parentStatement instanceof SwitchStatement) {
51+
CodeGenerator.ujp(CodeGenerator.getIfExitBranch(parentStatement.branchNum));
52+
53+
} else {
54+
CodeGenerator.ujp(CodeGenerator.getLoopEndBranch(parentStatement.branchNum));
55+
}
4556
}
4657
}

src/Syntax/Continue.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ void display(int lev) {
2222
System.out.println("Continue");
2323
}
2424

25+
/**
26+
* while, for문에서 continue가 쓰이는지 확인
27+
*/
28+
@Override
29+
protected void V(HashMap<String, Init> declarationMap, Type functionType) {
30+
check(false, "wrong statement. continue can not be here");
31+
}
32+
2533
@Override
2634
protected void V(HashMap<String, Init> declarationMap, Statement loopStatement, Type functionType) {
2735
V(declarationMap, loopStatement);

src/Syntax/DateValue.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,14 @@ protected void V(HashMap<String, Init> declarationMap) {
5151

5252
check(year >= 0, "year can not be negative value");
5353
check(month >= 0, "month can not be negative value");
54-
check(day >=0, "day can not be negative value");
54+
check(day >= 0, "day can not be negative value");
5555

5656
calendar = new GregorianCalendar(year, month - 1, day);
5757

5858
int rightYear = calendar.get(Calendar.YEAR);
5959
int rightMonth = calendar.get(Calendar.MONTH);
6060
int rightDay = calendar.get(Calendar.DATE);
6161

62-
System.out.println(year + " " + month + " " + day);
63-
System.out.println(calendar.getTime());
64-
6562
check(rightYear == year && rightMonth + 1 == month && rightDay == day,
6663
"wrong date type declaration. should be : " + rightYear + "/" + rightMonth + "/" + rightDay);
6764

src/Syntax/Declaration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ void display(int lev) {
3636
}
3737
}
3838

39+
/**
40+
* 선언에 대해서 확인, init V를 호출
41+
*/
3942
@Override
4043
protected void V(HashMap<String, Init> declarationMap) {
4144
for (Init init : inits) {

0 commit comments

Comments
 (0)