70 lines
2.0 KiB
Nim
70 lines
2.0 KiB
Nim
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
## The code generator for translating peon to C code
|
|
import std/tables
|
|
import std/strformat
|
|
import std/algorithm
|
|
import std/parseutils
|
|
import std/strutils
|
|
import std/sequtils
|
|
import std/sets
|
|
import std/os
|
|
|
|
|
|
import frontend/compiler/compiler
|
|
import frontend/parsing/lexer
|
|
import frontend/parsing/parser
|
|
import frontend/parsing/ast
|
|
|
|
|
|
type
|
|
CompilerFunc = object
|
|
## An internal compiler function called
|
|
## by pragmas
|
|
kind: PragmaKind
|
|
handler: proc (self: NativeCCompiler, pragma: Pragma, name: Name)
|
|
|
|
NativeCCompiler* = ref object of Compiler
|
|
## The peon to C compiler
|
|
|
|
# Compiler procedures called by pragmas
|
|
compilerProcs: TableRef[string, CompilerFunc]
|
|
|
|
|
|
proc newNativeCCompiler*(replMode: bool = false): NativeCCompiler =
|
|
## Initializes a new, blank, NativeCCompiler
|
|
## object
|
|
new(result)
|
|
result.ast = @[]
|
|
result.current = 0
|
|
result.file = ""
|
|
result.names = @[]
|
|
result.depth = 0
|
|
result.lines = @[]
|
|
result.currentFunction = nil
|
|
result.replMode = replMode
|
|
result.currentModule = nil
|
|
result.compilerProcs = newTable[string, CompilerFunc]()
|
|
result.source = ""
|
|
result.lexer = newLexer()
|
|
result.lexer.fillSymbolTable()
|
|
result.parser = newParser()
|
|
result.isMainModule = false
|
|
result.disabledWarnings = @[]
|
|
|
|
|
|
method literal*(self: Compiler, node: ASTNode, compile: bool = true): Type {.discardable.} =
|
|
## Compiles literal expressions
|
|
|