Skip to content

Commit

Permalink
Merge pull request #1 from kaushalmodi/fix-devel-compilation
Browse files Browse the repository at this point in the history
Fix compilation on nim devel as of 2019/05/12
  • Loading branch information
nc-x committed May 16, 2019
2 parents ac504f0 + 60cbc3f commit 16e94ec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/craftypkg/error.nim
@@ -1,7 +1,7 @@
import strformat

import token
import tokentype
import tokenType

var hadError* = false

Expand Down
48 changes: 24 additions & 24 deletions src/craftypkg/interpreter.nim
Expand Up @@ -3,9 +3,9 @@
import expr
import stmt
import types
import literaltype
import tokentype
import Token
import literalType
import tokenType
import token
import strutils
import runtimeerror
import environment
Expand Down Expand Up @@ -42,7 +42,7 @@ proc isTruthy(self: Interpreter, base: BaseType): bool
proc isEqual(a: BaseType, b: BaseType): bool
proc checkNumberOperand(self: Interpreter, operator: Token, operand: BaseType)
proc checkNumberOperands(self: Interpreter, operator: Token, left: BaseType, right: BaseType)
proc `$`(value: BaseType): string
proc `$`(value: BaseType): string
proc executeBlock(self: Interpreter, statements: seq[Stmt], environment: Environment)

# Proc
Expand All @@ -66,7 +66,7 @@ proc findMethod(self: ClassType, instance: ClassInstance, name: string): Functio
proc get*(self: ClassInstance, name: Token): BaseType =
if fields.contains(name.lexeme):
return fields[name.lexeme]

var m = class.findMethod(self, name.lexeme)
if m != nil: return m

Expand All @@ -80,7 +80,7 @@ proc set*(self: ClassInstance, name: Token, value: BaseType) =
proc newClass*(n: string, superclass: ClassType, m: Table[string, Function]): ClassType =
var c = ClassType(name: n, methods: m)
c.superclass = superclass
c.call =
c.call =
proc(interpreter: Interpreter, arguments: seq[BaseType]): BaseType =
var instance = newClassInstance(c)
var initializer = c.methods.getOrDefault("init", nil)
Expand All @@ -90,9 +90,9 @@ proc newClass*(n: string, superclass: ClassType, m: Table[string, Function]): Cl
c.arity =
proc(): int =
var initializer = c.methods.getOrDefault("init", nil)
if initializer == nil: return 0
if initializer == nil: return 0
return initializer.arity()

return c

proc newFuncType*(arity: () -> int, call: (Interpreter, seq[BaseType]) -> BaseType): FuncType =
Expand All @@ -103,11 +103,11 @@ proc newFunction*(declaration: FuncStmt, closure: Environment, isInitializer: bo
f.isInitializer = isInitializer
f.closure = closure
f.declaration = declaration
f.call =
f.call =
proc(interpreter: Interpreter, arguments: seq[BaseType]): BaseType =
var environment = newEnvironment(f.closure)
for i in 0 ..< f.declaration.parameters.len:
environment.define(f.declaration.parameters[i].lexeme, arguments[i])
environment.define(f.declaration.parameters[i].lexeme, arguments[i])
try:
interpreter.executeBlock(f.declaration.body, environment)
except ReturnException as r:
Expand Down Expand Up @@ -140,7 +140,7 @@ method evaluate*(self: Interpreter, expr: Assign): BaseType =
environment.assignAt(distance, expr.name, value)
else:
globals.assign(expr.name, value)

return value

method evaluate*(self: Interpreter, expr: Literal): BaseType =
Expand Down Expand Up @@ -175,15 +175,15 @@ method evaluate*(self: Interpreter, expr: Unary): BaseType =
return newNum(-NumType(right).value)
else:
discard

return nil

proc isTruthy(self: Interpreter, base: BaseType): bool =
if base of NilType:
return false
if base of BoolType:
return BoolType(base).value
return true
return true

method evaluate*(self: Interpreter, expr: Binary): BaseType =
var left = evaluate(expr.left)
Expand Down Expand Up @@ -220,18 +220,18 @@ method evaluate*(self: Interpreter, expr: Binary): BaseType =
return newNum(NumType(left).value + NumType(right).value)
if left of StrType and right of StrType:
return newStr(StrType(left).value & StrType(right).value)
raise newRuntimeError(expr.operator, "Operands must be either two number or two strings.")
raise newRuntimeError(expr.operator, "Operands must be either two number or two strings.")
else:
discard

return nil

proc isEqual(a: BaseType, b: BaseType): bool =
if a of NilType and b of NilType: return true

if a of BoolType and b of BoolType:
return BoolType(a).value == BoolType(b).value

if a of NumType and b of NumType:
return NumType(a).value == NumType(b).value

Expand All @@ -255,7 +255,7 @@ method evaluate*(self: Interpreter, expr: Call): BaseType =
var arguments: seq[BaseType]
for argument in expr.arguments:
arguments.add(self.evaluate(argument))

if not(callee of FuncType):
raise newRuntimeError(expr.paren, "Can only call functions and classes.")

Expand All @@ -278,7 +278,7 @@ method evaluate*(self: Interpreter, expr: GetExpr): BaseType =
var obj = evaluate(expr.obj)
if obj of ClassInstance:
return ClassInstance(obj).get(expr.name)

raise newRuntimeError(expr.name, "Only instances have properties.")

method evaluate*(self: Interpreter, expr: SetExpr): BaseType =
Expand Down Expand Up @@ -333,7 +333,7 @@ method evaluate*(self: Interpreter, stmt: VarStmt) =
var value: BaseType = nil
if stmt.initializer != nil:
value = evaluate(stmt.initializer)

environment.define(stmt.name.lexeme, value)

method evaluate*(self: Interpreter, stmt: WhileStmt) =
Expand All @@ -360,10 +360,10 @@ method evaluate*(self: Interpreter, stmt: ClassStmt) =
methods.add(m.name.lexeme, function)

var class = newClass(stmt.name.lexeme, ClassType(superclass), methods)

if superclass != nil:
environment = environment.enclosing

environment.assign(stmt.name, class)

method evaluate*(self: Interpreter, stmt: BlockStmt) =
Expand Down Expand Up @@ -393,10 +393,10 @@ proc `$`(value: BaseType): string =
if text.endsWith(".0"):
text = text[0 .. ^3]
return text

if value of StrType:
return StrType(value).value

if value of BoolType:
return $BoolType(value).value

Expand Down
2 changes: 1 addition & 1 deletion src/craftypkg/objhashes.nim
@@ -1,4 +1,4 @@
import expr, hashes, literaltype, token
import expr, hashes, literalType, token

method hash*(x: Expr): Hash {.base.} = discard

Expand Down
30 changes: 15 additions & 15 deletions src/craftypkg/parser.nim
@@ -1,9 +1,9 @@
{.this: self.}

import token
import tokentype
import tokenType
import expr
import literaltype
import literalType
import stmt
from error import nil

Expand Down Expand Up @@ -84,7 +84,7 @@ proc assignment(self: var Parser): Expr =
elif expr of GetExpr:
var get = GetExpr(expr)
return newSetExpr(get.obj, get.name, value)

error.error(equals, "Invalid assignment target.")

return expr
Expand All @@ -94,7 +94,7 @@ proc expression(self: var Parser): Expr =

proc equality(self: var Parser): Expr =
var expr = comparison()

while match(BANG_EQUAL, EQUAL_EQUAL):
var operator = previous()
var right = comparison()
Expand All @@ -107,7 +107,7 @@ proc match(self: var Parser, types: varargs[TokenType]): bool =
if check(ty):
discard advance()
return true

return false

proc check(self: var Parser, tokentype: TokenType): bool =
Expand Down Expand Up @@ -144,9 +144,9 @@ proc addition(self: var Parser): Expr =
while match(MINUS, PLUS):
var operator = previous()
var right = multiplication()

expr = newBinary(expr, operator, right)

return expr


Expand Down Expand Up @@ -180,7 +180,7 @@ proc finishCall(self: var Parser, callee: Expr): Expr =
if arguments.len >= 8:
error.error(peek(), "Cannot have more than 8 arguments.")
arguments.add(expression())

var paren = consume(RIGHT_PAREN, "Expect ')' after arguments.")

return newCall(callee, paren, arguments)
Expand Down Expand Up @@ -218,12 +218,12 @@ proc primary(self: var Parser): Expr =

if match(IDENTIFIER):
return newVariable(previous())

if match(LEFT_PAREN):
var expr = expression()
discard consume(RIGHT_PAREN, "Expect ')' after expression")
return newGrouping(expr)

raise error(peek(), "Expect expression.")

proc consume(self: var Parser, tktype: TokenType, message: string): Token =
Expand Down Expand Up @@ -292,7 +292,7 @@ proc forStatement(self: var Parser): Stmt =
var condition: Expr = nil
if not check(SEMICOLON):
condition = expression()

discard consume(SEMICOLON, "Expect ';' after loop condition.")

var increment: Expr = nil
Expand All @@ -319,7 +319,7 @@ proc returnStatement(self: var Parser): Stmt =
var value: Expr
if not check(SEMICOLON):
value = expression()

discard consume(SEMICOLON, "Expect ';' after return value.")
return newReturnStmt(keyword, value)

Expand Down Expand Up @@ -361,7 +361,7 @@ proc function(self: var Parser, kind: string): FuncStmt =
if parameters.len >= 8:
error.error(peek(), "Cannot have more than 8 parameters.")
parameters.add(consume(IDENTIFIER, "Expect parameter name."))

discard consume(RIGHT_PAREN, "Expect ')' after parameters.")

discard consume(LEFT_BRACE, "Expect '{' before " & kind & " body.")
Expand Down Expand Up @@ -404,7 +404,7 @@ proc varDeclaration(self: var Parser): Stmt =
initializer = expression()
else:
initializer = nil

discard consume(SEMICOLON, "Expect ';' after variable declaration.")
return newVarStmt(name, initializer)

Expand All @@ -413,5 +413,5 @@ proc parse*(self: var Parser): seq[Stmt] =
var statements: seq[Stmt]
while not isAtEnd():
statements.add(declaration())

return statements
4 changes: 2 additions & 2 deletions src/craftypkg/resolver.nim
Expand Up @@ -7,7 +7,7 @@ import token
import error
import objhashes
import interpreterObj
import literaltype
import literalType

type
Resolver* = ref object of RootObj
Expand Down Expand Up @@ -137,7 +137,7 @@ method resolve(self: var Resolver, stmt: ClassStmt) =
if m.name.lexeme == "init":
declaration = INITIALIZER
resolveFunction(m, declaration)

endScope()

if stmt.superclass != nil: endScope()
Expand Down

0 comments on commit 16e94ec

Please sign in to comment.