Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ToucheSir committed Feb 26, 2018
1 parent eaeb780 commit feed8fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 106 deletions.
16 changes: 12 additions & 4 deletions src/main/antlr3/parser/UnnamedLanguage.g
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,23 @@ expressionStatement returns [Statement s]

block returns [Block b]
@init { b = new Block(); }
: '{' (s = statement { b.add(s); })* '}'
: '{' (s = statement { if (s != null) b.add(s); })* '}'
;

expr returns [Expression e]
: lhs = exprLessThan { e = lhs; }
(OP_EQUALS rhs = exprLessThan { e = new EqualityExpression(e, rhs); })*
(op = OP_EQUALS rhs = exprLessThan {
e = new EqualityExpression(e, rhs);
setLineOffset(e, $op);
})*
;

exprLessThan returns [Expression e]
: lhs = exprPlusMinus { e = lhs; }
(OP_LESSTHAN rhs = exprPlusMinus { e = new LessThanExpression(e, rhs); })*
(op = OP_LESSTHAN rhs = exprPlusMinus {
e = new LessThanExpression(e, rhs);
setLineOffset(e, $op);
})*
;

exprPlusMinus returns [Expression e]
Expand All @@ -210,12 +216,14 @@ exprPlusMinus returns [Expression e]
} else if ($op.type == OP_MINUS) {
e = new SubtractExpression(e, rhs);
}
setLineOffset(e, $op);
})*
;

exprMul returns [Expression e]
: lhs = atom { e = lhs; } (OP_MUL rhs = atom {
: lhs = atom { e = lhs; } (op = OP_MUL rhs = atom {
e = new MultExpression(e, rhs);
setLineOffset(e, $op);
})*
;

Expand Down
147 changes: 45 additions & 102 deletions src/test/java/semantic/TypeCheckerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,107 +415,50 @@ class TypeCheckerTest {
)

@Test
fun `5-2 An array index expression must have type int`() = check(
"""
void main() {
int[1] a;
a[foo()];
}
string foo() { return ""; }
""".trimIndent(),
"cannot index array with expression of type string", 3, 9
)
fun `5-2 An array index expression must have type int`() {
check(
"""
void main() {
int[1] a;
a[foo()];
}
string foo() { return ""; }
""".trimIndent(),
"cannot index array with expression of type string", 3, 9
)
check(
"""
void main() {
int[1] a;
a[bar()];
}
int bar() { return 0; }
""".trimIndent()
)
}

/*
(5) The type of hhhj
containing function.
(6) For an assignment e 1 =e 2 ,
type(e 1 ) ≥ type(e 2 )
3.2. Expressions.
(1) For an array index expression e,
type(e) = int
(2) For an expression e 1 ⊕ e 2 :
(a) If ⊕ is +:
int
float
int
int
float
float
char
string
boolean
void
char
string
boolean void
boolean void
char
string
(b) If ⊕ is -:
int
float
char
string
boolean
void
int
int
float
char
string
float
char
2(c) If ⊕ is *:
int
float
char
string
boolean
void
int
int
float
char string boolean void
char string boolean void
float
(d) If ⊕ is <:
int
float
char
string
boolean
void
int
boolean
float
boolean
boolean
boolean
boolean
(e) If ⊕ is ==:
int
float
char
string
boolean
void
int
boolean
float
char
string
boolean
void
boolean
boolean
boolean
boolean
(3) For an invocation of function f,
(a) The number of arguments in the invocation must match the number of parameters n, in the
function f.
(b) Denote the parameters of f as p i , i ≤ n. Similarly, denote the arguments of a call to f as a i ,
i ≤ n. For each i, we must have
type(a i ) ≤ type(p i )
*/
@Test
fun `5-3 An add expression must have have the correct operand and result types`() {
val types = arrayOf(IntegerType, FloatType, CharType, StringType, BooleanType)
val typeTable = arrayOf(
Triple(IntegerType, IntegerType, IntegerType)
)
val op = "+"
val template =
"""
void main() {
${types.joinToString("\n") { "$it ${it}Var;" }}
%s
}
"""
for (t1 in types) for (t2 in types) types
.filter { Triple(t1, t2, it) !in typeTable }
.forEach {
check(
template.format("${it}Var = ${t1}Var $op ${t2}Var;"),
"cannot apply operator $op to types $t1 and $t2",
7, " ${it}Var = ${t1}Var ".length
)
}
}
}

0 comments on commit feed8fc

Please sign in to comment.