imports are now python style with a twist: forms! import std ffi and import local myfile

This commit is contained in:
2023-09-06 22:43:03 -07:00
parent 0e4362eefb
commit 81296ee1d1
22 changed files with 58 additions and 50 deletions

View File

@ -183,11 +183,9 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
private fun readImportDeclaration(): ImportDeclaration = within {
expect(TokenType.Import)
var form: Symbol? = null
if (peek(TokenType.Symbol)) {
form = readSymbolRaw()
}
ImportDeclaration(form, readStringLiteral())
val form = readSymbolRaw()
val components = oneAndContinuedBy(TokenType.Period) { readSymbolRaw() }
ImportDeclaration(form, components)
}
private fun readFunctionDeclaration(): FunctionDefinition = within {
@ -292,11 +290,21 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
): List<T> {
val items = mutableListOf<T>()
while (!peek(peeking)) {
val expression = read()
val item = read()
if (consuming != null) {
next(consuming)
}
items.add(expression)
items.add(item)
}
return items
}
private fun <T> oneAndContinuedBy(separator: TokenType, read: () -> T): List<T> {
val items = mutableListOf<T>()
items.add(read())
while (peek(separator)) {
expect(separator)
items.add(read())
}
return items
}

View File

@ -166,7 +166,14 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
override fun visitImportDeclaration(node: ImportDeclaration) {
append("import ")
visit(node.path)
visit(node.form)
append(" ")
for ((index, component) in node.components.withIndex()) {
visit(component)
if (index != node.components.size - 1) {
append(".")
}
}
}
override fun visitCompilationUnit(node: CompilationUnit) {

View File

@ -22,6 +22,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
RightParentheses(SingleChar(')')),
Negation(SingleChar('!'), Promotion('=', Inequality), OperatorFamily),
Comma(SingleChar(',')),
Period(SingleChar('.')),
False(Keyword("false"), KeywordFamily),
True(Keyword("true"), KeywordFamily),
If(Keyword("if"), KeywordFamily),