Browse Source

Added compilation mode flag

master
Mattia Giambirtone 2 weeks ago
parent
commit
b99be47556
  1. 2
      src/config.nim
  2. 14
      src/frontend/compiler.nim
  3. 19
      src/frontend/meta/ast.nim
  4. 1
      src/frontend/parser.nim
  5. 4
      src/main.nim

2
src/config.nim

@ -60,6 +60,8 @@ Options
-b, --breakpoints Run the debugger at specific bytecode offsets (comma-separated).
Only available when compiled with VM debugging on
-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
yes/on and no/off
--noWarn Disable a specific warning (for example, --noWarn unusedVariable)

14
src/frontend/compiler.nim

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

19
src/frontend/meta/ast.nim

@ -156,7 +156,7 @@ type
callee*: Expression # The object being called
arguments*: tuple[positionals: seq[Expression], keyword: seq[tuple[
name: IdentExpr, value: Expression]]]
genericArgs*: seq[Expression]
closeParen*: Token # Needed for error reporting
UnaryExpr* = ref object of Expression
operator*: Token
@ -454,12 +454,11 @@ proc newSetItemExpr*(obj: Expression, name: IdentExpr, value: Expression,
proc newCallExpr*(callee: Expression, arguments: tuple[positionals: seq[
Expression], keyword: seq[tuple[name: IdentExpr, value: Expression]]],
token: Token, genericArgs: seq[Expression] = @[]): CallExpr =
token: Token): CallExpr =
result = CallExpr(kind: callExpr)
result.callee = callee
result.arguments = arguments
result.token = token
result.genericArgs = @[]
proc newSliceExpr*(expression: Expression, ends: seq[Expression], token: Token): SliceExpr =
@ -794,9 +793,21 @@ proc getRelativeBoundaries*(self: ASTNode): tuple[start, stop: int] =
of unaryExpr:
var self = UnaryExpr(self)
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)
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:
var self = Pragma(self)
let start = self.token.relPos.start

1
src/frontend/parser.nim

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

4
src/main.nim

@ -152,7 +152,7 @@ proc repl =
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
tokens: seq[Token] = @[]
tree: seq[Declaration] = @[]
@ -197,7 +197,7 @@ proc runFile(f: string, fromString: bool = false, dump: bool = true, breakpoints
for node in tree:
styledEcho fgGreen, "\t", $node
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:
styledEcho fgCyan, "Compilation step:\n"
debugger.disassembleChunk(compiled, f)

Loading…
Cancel
Save