Skip to content

Commit

Permalink
🐛 Fixing a bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
JYInMyHeart committed Jul 3, 2019
1 parent 6cf952f commit 0294505
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 72 deletions.
61 changes: 56 additions & 5 deletions src/main/resources/test.xck
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
def in(a:int):int{
return a * a
}

def out(a:int):int{
return in(a) * in(a)
}

var a = out(10)

mPrint(intToStr(a / 100))


def sum(n: int): int
{
var i = 0
var result = 0
while (true)
{
if (i == n) return result
result = result + i
i = i + 1
}
var z = 0
}

mPrint(intToStr(sum(100)))

def test(a:double,b:bool):void{
if(b){
mPrint(doubleToStr(a + 1))
}else{
mPrint(doubleToStr(a - 1))
}
}
let b = inputDouble()
let c = inputBool()
test(b,c)


for d = 0 to 10 {
if(d == 4){
mPrint("d is 4!")
break
}else{
mPrint(intToStr(d))
}
}


for d = 0 to 10 {
if(d == 4){
continue

}
mPrint(intToStr(d))

def f(a:Double):Double = {
a + 1
}
f(3)
}
}
34 changes: 7 additions & 27 deletions src/main/resources/test1.xck
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
{
var d = 0
while (d < 10) {
d = d + 1


def f1(a:Integer):Double = {

def f2(a:Integer):Double = {
a + 3
}
a + f2(a)
if(d == 6){
continue
}
mPrint(intToStr(d))

var a = 5

var b = f1(a)

def sum(n:Double):Double = {
var sum = 0
for i = 0 to n {
sum = sum + i
}
sum
}

sum(b) + 1


f1(sum(b) + 2 - 3) / 4 > 5 & 2 > 3

f1(3)
}
}
4 changes: 2 additions & 2 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ object Main {
)
} else {
previous = compilation
println(result.value)
}
}
}
Expand Down Expand Up @@ -70,7 +69,8 @@ object Main {
}

def main(args: Array[String]): Unit = {
commandLine()
// commandLine()

loadFile("test1.xck")
}
}
18 changes: 11 additions & 7 deletions src/main/scala/binder/Binder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ case class Binder(parent: BindScope, function: FunctionSymbol) {
TypeSymbol.Bool
case "int" =>
TypeSymbol.Int
case "String" =>
case "string" =>
TypeSymbol.String
case "void" =>
TypeSymbol.Void
case "double" =>
TypeSymbol.Double
case _ => null
}

Expand Down Expand Up @@ -50,14 +54,14 @@ case class Binder(parent: BindScope, function: FunctionSymbol) {
}
val bindType = bindTypeClause(node.functionType)
val functionType = if (bindType == null) TypeSymbol.Void else bindType
val declaredFuntion =
val declaredFunction =
new FunctionSymbol(node.identifier.text,
parameters.toList,
functionType,
node)
if (!scope.tryDeclareFunction(declaredFuntion))
if (!scope.tryDeclareFunction(declaredFunction))
diagnostics.reportFunctionAlreadyDeclared(node.identifier.span,
declaredFuntion.name)
declaredFunction.name)
}

val diagnostics: DiagnosticsBag = DiagnosticsBag()
Expand Down Expand Up @@ -114,7 +118,7 @@ case class Binder(parent: BindScope, function: FunctionSymbol) {
statement.keyword.text)
return bindErrorStatement()
}
val continueLabel = loopStack.head._1
val continueLabel = loopStack.head._2
BindGotoStatement(continueLabel)
}

Expand All @@ -136,7 +140,7 @@ case class Binder(parent: BindScope, function: FunctionSymbol) {
bindForStatement(s)
case (TokenType.returnStatement, s: ReturnStatement) =>
bindReturnStatement(s)
case (TokenType.breakKeyword, s: BreakStatement) =>
case (TokenType.breakStatement, s: BreakStatement) =>
bindBreakStatement(s)
case (TokenType.continueStatement, s: ContinueStatement) =>
bindContinueStatement(s)
Expand Down Expand Up @@ -589,7 +593,7 @@ abstract class BindTreeRewriter {
builder += node.paramList(j)
}
}
if (builder.isEmpty)
if (builder.nonEmpty)
builder += newArguement
}
if (builder.isEmpty)
Expand Down
18 changes: 15 additions & 3 deletions src/main/scala/eval/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Eval(program: BindProgram) {
for (i <- body.bindStatements.indices) {
body.bindStatements(i) match {
case statement: BindLabelStatement =>
labelToIndex += statement.label -> (i + 1)
labelToIndex += statement.label -> (i)
case _ =>
}
}
Expand All @@ -47,7 +47,13 @@ class Eval(program: BindProgram) {
index += 1
case (BindType.gotoStatement, s: BindGotoStatement) =>
index = labelToIndex(s.label)
case (BindType.labelStatement, s: BindLabelStatement) =>
case (BindType.conditionGotoStatement, s: BindConditionGotoStatement) =>
val condition = evalExpression(s.condition)
if (condition == s.jumpIfTrue)
index = labelToIndex(s.label)
else
index += 1
case (BindType.labelStatement, _: BindLabelStatement) =>
index += 1
case (BindType.returnStatement, s: BindReturnStatement) =>
lastValue =
Expand Down Expand Up @@ -169,14 +175,20 @@ class Eval(program: BindProgram) {
value
case node: BindFuncCallExpression =>
node.functionSymbol match {
case BuiltinFunctions.input => StdIn.readLine()
case BuiltinFunctions.input => StdIn.readLine()
case BuiltinFunctions.inputBool => StdIn.readLine().toBoolean
case BuiltinFunctions.inputDouble => StdIn.readLine().toDouble
case BuiltinFunctions.inputInt => StdIn.readLine().toInt
case BuiltinFunctions.mPrint =>
val msg = evalExpression(node.paramList.head)
println(msg)
null
case BuiltinFunctions.rnd =>
val max = evalExpression(node.paramList.head)
random.nextInt(max.asInstanceOf[Int])
case BuiltinFunctions.doubleToStr | BuiltinFunctions.boolToStr |
BuiltinFunctions.intToStr =>
evalExpression(node.paramList.head).toString
case _ =>
val local = mutable.HashMap[VariableSymbol, Any]()
for (i <- node.paramList.indices) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/lowering/Lowerer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Lowerer extends BindTreeRewriter {
val elseLabel = generateLabel()
val endLabel = generateLabel()
val gotoFalse =
BindConditionGotoStatement(endLabel, n.condition, jumpIfTrue = false)
BindConditionGotoStatement(elseLabel, n.condition, jumpIfTrue = false)
val gotoEndStatement = BindGotoStatement(endLabel)
val elseLabelStatement = BindLabelStatement(elseLabel)
val endLabelStatement = BindLabelStatement(endLabel)
Expand Down Expand Up @@ -121,10 +121,8 @@ class Lowerer extends BindTreeRewriter {
increment
)
)
val whileStatement = BindWhileStatement(condition,
whileBody,
node.breakLabel,
node.continueLabel)
val whileStatement =
BindWhileStatement(condition, whileBody, node.breakLabel, generateLabel())
val result = BindBlockStatement(
List(
variableDeclaration,
Expand Down
56 changes: 34 additions & 22 deletions src/main/scala/parser/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class Parser(sourceText: SourceText) {
if (kind == current.getKind)
return nextToken
diagnostics.reportUnexpectedToken(current.span, current.getKind, kind)
Token(kind, current.position, null, null)
val previousToken = current
nextToken
Token(kind, previousToken.position, null, null)
}

def parseGlobalStatement(): GlobalStatementNode = {
Expand Down Expand Up @@ -201,25 +203,31 @@ class Parser(sourceText: SourceText) {
ParamStatement(paramName, paramType)
}

def parseFuncStatement(): FuncStatement = {
val func = eat(funcKeyword)
val id = eat(funcCallExpression)
eat(openParenthesisToken)
var parameters: List[ParamStatement] = List()
while (current.tokenType != closeParenthesisToken) {
parameters :+= parseParamStatement()
}
eat(closeParenthesisToken)
eat(colonToken)
val paramType = eat(TokenType.typeToken)
val returnType =
ParamStatement(
Token(returnKeyword, current.position, current.text, current.value),
paramType)
eat(equalsToken)
val body = parseStatement()
FuncStatement(func, id, parameters, returnType, body)
}
// def parseFuncStatement(): FuncStatement = {
// val func = eat(funcKeyword)
// val id = eat(funcCallExpression)
// eat(openParenthesisToken)
// var parameters: List[ParamStatement] = List()
// var firstParam = true
// while (current.tokenType != closeParenthesisToken) {
// if (firstParam) {
// firstParam = false
// } else {
// eat(TokenType.commaToken)
// }
// parameters :+= parseParamStatement()
// }
// eat(closeParenthesisToken)
// eat(colonToken)
// val paramType = eat(TokenType.typeToken)
// val returnType =
// ParamStatement(
// Token(returnKeyword, current.position, current.text, current.value),
// paramType)
// eat(equalsToken)
// val body = parseStatement()
// FuncStatement(func, id, parameters, returnType, body)
// }

def parseBreakStatement(): Statement = {
val keyword = eat(TokenType.breakKeyword)
Expand Down Expand Up @@ -255,8 +263,6 @@ class Parser(sourceText: SourceText) {
parseWhileStatement()
case TokenType.forKeyword =>
parseForStatement()
case TokenType.funcKeyword =>
parseFuncStatement()
case TokenType.breakKeyword =>
parseBreakStatement()
case TokenType.continueKeyword =>
Expand Down Expand Up @@ -358,7 +364,13 @@ class Parser(sourceText: SourceText) {

def parseArgument(): List[Expression] = {
var paramsList = List[Expression]()
var firstArguement = true
while (current.getKind != closeParenthesisToken && current.getKind != eofToken) {
if (firstArguement) {
firstArguement = false
} else {
eat(TokenType.commaToken)
}
val expression = parseExpression()
paramsList :+= expression
}
Expand Down
31 changes: 30 additions & 1 deletion src/main/scala/symbol/BuiltinFunctions.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package symbol


object BuiltinFunctions {
val mPrint: FunctionSymbol = new FunctionSymbol(
"mPrint",
List(new ParameterSymbol("text", TypeSymbol.String)),
TypeSymbol.Void)
val input: FunctionSymbol =
new FunctionSymbol("input", List(), TypeSymbol.String)
val inputInt: FunctionSymbol =
new FunctionSymbol("inputInt", List(), TypeSymbol.Int)
val inputDouble: FunctionSymbol =
new FunctionSymbol("inputDouble", List(), TypeSymbol.Double)
val inputBool: FunctionSymbol =
new FunctionSymbol("inputBool", List(), TypeSymbol.Bool)
val rnd: FunctionSymbol = new FunctionSymbol(
"rnd",
List(new ParameterSymbol("max", TypeSymbol.Int)),
TypeSymbol.Int)

val doubleToStr: FunctionSymbol = new FunctionSymbol(
"doubleToStr",
List(
new ParameterSymbol("str", TypeSymbol.Double)
),
TypeSymbol.String
)

val intToStr: FunctionSymbol = new FunctionSymbol(
"intToStr",
List(
new ParameterSymbol("int", TypeSymbol.Int)
),
TypeSymbol.String
)

val boolToStr: FunctionSymbol = new FunctionSymbol(
"boolToStr",
List(
new ParameterSymbol("bool", TypeSymbol.Bool)
),
TypeSymbol.String
)

def getAll: List[FunctionSymbol] =
BuiltinFunctions.getClass.getDeclaredFields
.filter(_.getType.equals(classOf[FunctionSymbol]))
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/symbol/TypeSymbol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ object TypeSymbol {
val Double = new TypeSymbol("double")
val String = new TypeSymbol("string")
val Void = new TypeSymbol("void")
val Object = new TypeSymbol("object")

}

0 comments on commit 0294505

Please sign in to comment.