peon-rewrite/src/main.nim

70 lines
2.2 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.
import util/fmterr
import util/symbols
import frontend/parsing/lexer
import frontend/parsing/parser
import frontend/compiler/compiler
import std/strformat
proc `$`(self: TypedNode): string =
if self.node.isConst():
var self = TypedExpr(self)
return &"{self.node}: {self.kind[]}"
case self.node.kind:
of varDecl, typeDecl, funDecl:
var self = TypedDecl(self)
result = &"{self.name[]}: {self.name.valueType[]}"
of identExpr, binaryExpr, unaryExpr:
var self = TypedExpr(self)
result &= &"{self.node}: {self.kind[]}"
else:
result = &"{self.node}: ? ({self.node.kind})"
proc main =
var
lexer = newLexer()
parser = newParser()
compiler = newPeonCompiler()
source: string
file = "test.pn"
lexer.fillSymbolTable()
while true:
stdout.write(">>> ")
stdout.flushFile()
try:
source = stdin.readLine()
for typedNode in compiler.compile(parser.parse(lexer.lex(source, file), file, lexer.getLines(), lexer.getSource()), lexer.getFile(), lexer.getSource(),
showMismatches=true):
echo &"{typedNode.node} -> {compiler.stringify(typedNode)}\n"
except IOError:
echo ""
break
except LexingError as exc:
print(exc)
except ParseError as exc:
print(exc)
except CompileError as exc:
print(exc)
when isMainModule:
setControlCHook(proc () {.noconv.} = echo ""; quit(0))
main()