Added compilation mode flag

This commit is contained in:
Mattia Giambirtone 2022-11-23 10:40:45 +01:00
parent ab30d0d891
commit b99be47556
5 changed files with 31 additions and 9 deletions

View File

@ -60,6 +60,8 @@ Options
-b, --breakpoints Run the debugger at specific bytecode offsets (comma-separated). -b, --breakpoints Run the debugger at specific bytecode offsets (comma-separated).
Only available when compiled with VM debugging on Only available when compiled with VM debugging on
-d, --disassemble Disassemble the given bytecode file instead of executing it -d, --disassemble Disassemble the given bytecode file instead of executing it
-m, --mode Set the compilation mode. Acceptable values are 'debug' and
'release'
--warnings Turn warnings on/off (default: on). Acceptable values are --warnings Turn warnings on/off (default: on). Acceptable values are
yes/on and no/off yes/on and no/off
--noWarn Disable a specific warning (for example, --noWarn unusedVariable) --noWarn Disable a specific warning (for example, --noWarn unusedVariable)

View File

@ -93,6 +93,9 @@ type
WarningKind* {.pure.} = enum WarningKind* {.pure.} = enum
## A warning enumeration type ## A warning enumeration type
UnreachableCode, UnusedName, ShadowOuterScope UnreachableCode, UnusedName, ShadowOuterScope
CompileMode* {.pure.} = enum
## A compilation mode enumeration
Debug, Release
NameKind {.pure.} = enum NameKind {.pure.} = enum
## A name enumeration type ## A name enumeration type
None, Module, Argument, Var, Function, CustomType, Enum None, Module, Argument, Var, Function, CustomType, Enum
@ -225,6 +228,8 @@ type
# Whether to show detailed info about type # Whether to show detailed info about type
# mismatches when we dispatch with matchImpl() # mismatches when we dispatch with matchImpl()
showMismatches: bool showMismatches: bool
# Are we compiling in debug mode?
mode: CompileMode
PragmaKind = enum PragmaKind = enum
## An enumeration of pragma types ## An enumeration of pragma types
Immediate, Immediate,
@ -242,7 +247,8 @@ type
# Forward declarations # Forward declarations
proc compile*(self: Compiler, ast: seq[Declaration], file: string, lines: seq[tuple[start, stop: int]], source: string, chunk: Chunk = nil, proc compile*(self: Compiler, ast: seq[Declaration], file: string, lines: seq[tuple[start, stop: int]], source: string, chunk: Chunk = nil,
incremental: bool = false, isMainModule: bool = true, disabledWarnings: seq[WarningKind] = @[], showMismatches: bool = false): Chunk incremental: bool = false, isMainModule: bool = true, disabledWarnings: seq[WarningKind] = @[], showMismatches: bool = false,
mode: CompileMode = Debug): Chunk
proc expression(self: Compiler, node: Expression) proc expression(self: Compiler, node: Expression)
proc statement(self: Compiler, node: Statement) proc statement(self: Compiler, node: Statement)
proc declaration(self: Compiler, node: Declaration) proc declaration(self: Compiler, node: Declaration)
@ -2460,7 +2466,8 @@ proc declaration(self: Compiler, node: Declaration) =
proc compile*(self: Compiler, ast: seq[Declaration], file: string, lines: seq[tuple[start, stop: int]], source: string, chunk: Chunk = nil, proc compile*(self: Compiler, ast: seq[Declaration], file: string, lines: seq[tuple[start, stop: int]], source: string, chunk: Chunk = nil,
incremental: bool = false, isMainModule: bool = true, disabledWarnings: seq[WarningKind] = @[], showMismatches: bool = false): Chunk = incremental: bool = false, isMainModule: bool = true, disabledWarnings: seq[WarningKind] = @[], showMismatches: bool = false,
mode: CompileMode = Debug): Chunk =
## Compiles a sequence of AST nodes into a chunk ## Compiles a sequence of AST nodes into a chunk
## object ## object
if chunk.isNil(): if chunk.isNil():
@ -2478,6 +2485,7 @@ proc compile*(self: Compiler, ast: seq[Declaration], file: string, lines: seq[tu
self.isMainModule = isMainModule self.isMainModule = isMainModule
self.disabledWarnings = disabledWarnings self.disabledWarnings = disabledWarnings
self.showMismatches = showMismatches self.showMismatches = showMismatches
self.mode = mode
if not incremental: if not incremental:
self.jumps = @[] self.jumps = @[]
let pos = self.beginProgram() let pos = self.beginProgram()
@ -2516,7 +2524,7 @@ proc compileModule(self: Compiler, moduleName: string) =
path, self.lexer.getLines(), path, self.lexer.getLines(),
source, persist=true), source, persist=true),
path, self.lexer.getLines(), source, chunk=self.chunk, incremental=true, path, self.lexer.getLines(), source, chunk=self.chunk, incremental=true,
isMainModule=false, self.disabledWarnings, self.showMismatches) isMainModule=false, self.disabledWarnings, self.showMismatches, self.mode)
self.depth = 0 self.depth = 0
self.current = current self.current = current
self.ast = ast self.ast = ast

View File

@ -156,7 +156,7 @@ type
callee*: Expression # The object being called callee*: Expression # The object being called
arguments*: tuple[positionals: seq[Expression], keyword: seq[tuple[ arguments*: tuple[positionals: seq[Expression], keyword: seq[tuple[
name: IdentExpr, value: Expression]]] name: IdentExpr, value: Expression]]]
genericArgs*: seq[Expression] closeParen*: Token # Needed for error reporting
UnaryExpr* = ref object of Expression UnaryExpr* = ref object of Expression
operator*: Token operator*: Token
@ -454,12 +454,11 @@ proc newSetItemExpr*(obj: Expression, name: IdentExpr, value: Expression,
proc newCallExpr*(callee: Expression, arguments: tuple[positionals: seq[ proc newCallExpr*(callee: Expression, arguments: tuple[positionals: seq[
Expression], keyword: seq[tuple[name: IdentExpr, value: Expression]]], Expression], keyword: seq[tuple[name: IdentExpr, value: Expression]]],
token: Token, genericArgs: seq[Expression] = @[]): CallExpr = token: Token): CallExpr =
result = CallExpr(kind: callExpr) result = CallExpr(kind: callExpr)
result.callee = callee result.callee = callee
result.arguments = arguments result.arguments = arguments
result.token = token result.token = token
result.genericArgs = @[]
proc newSliceExpr*(expression: Expression, ends: seq[Expression], token: Token): SliceExpr = proc newSliceExpr*(expression: Expression, ends: seq[Expression], token: Token): SliceExpr =
@ -794,9 +793,21 @@ proc getRelativeBoundaries*(self: ASTNode): tuple[start, stop: int] =
of unaryExpr: of unaryExpr:
var self = UnaryExpr(self) var self = UnaryExpr(self)
result = (self.operator.relPos.start, getRelativeBoundaries(self.a).stop) result = (self.operator.relPos.start, getRelativeBoundaries(self.a).stop)
of intExpr, binExpr, hexExpr, octExpr, strExpr: of binaryExpr:
var self = BinaryExpr(self)
result = (getRelativeBoundaries(self.a).start, getRelativeBoundaries(self.b).stop)
of intExpr, binExpr, hexExpr, octExpr, strExpr, floatExpr:
var self = LiteralExpr(self) var self = LiteralExpr(self)
result = self.literal.relPos result = self.literal.relPos
of identExpr:
var self = IdentExpr(self)
result = self.token.relPos
of assignExpr:
var self = AssignExpr(self)
result = (getRelativeBoundaries(self.name).start, getRelativeBoundaries(self.value).stop)
of callExpr:
var self = CallExpr(self)
result = (getRelativeBoundaries(self.callee).start, self.closeParen.relPos.stop)
of pragmaExpr: of pragmaExpr:
var self = Pragma(self) var self = Pragma(self)
let start = self.token.relPos.start let start = self.token.relPos.start

View File

@ -422,6 +422,7 @@ proc makeCall(self: Parser, callee: Expression): CallExpr =
argCount += 1 argCount += 1
self.expect(RightParen) self.expect(RightParen)
result = newCallExpr(callee, arguments, tok) result = newCallExpr(callee, arguments, tok)
result.closeParen = self.peek(-1)
proc parseGenericArgs(self: Parser) = proc parseGenericArgs(self: Parser) =

View File

@ -152,7 +152,7 @@ proc repl =
proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints: seq[uint64] = @[], dis: bool = false, proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints: seq[uint64] = @[], dis: bool = false,
warnings: seq[WarningKind] = @[], mismatches: bool = false) = warnings: seq[WarningKind] = @[], mismatches: bool = false, mode: CompileMode = Debug) =
var var
tokens: seq[Token] = @[] tokens: seq[Token] = @[]
tree: seq[Declaration] = @[] tree: seq[Declaration] = @[]
@ -197,7 +197,7 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints
for node in tree: for node in tree:
styledEcho fgGreen, "\t", $node styledEcho fgGreen, "\t", $node
echo "" echo ""
compiled = compiler.compile(tree, f, tokenizer.getLines(), input, disabledWarnings=warnings, showMismatches=mismatches) compiled = compiler.compile(tree, f, tokenizer.getLines(), input, disabledWarnings=warnings, showMismatches=mismatches, mode=mode)
when debugCompiler: when debugCompiler:
styledEcho fgCyan, "Compilation step:\n" styledEcho fgCyan, "Compilation step:\n"
debugger.disassembleChunk(compiled, f) debugger.disassembleChunk(compiled, f)