mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
imports are now python style with a twist: forms! import std ffi and import local myfile
This commit is contained in:
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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),
|
||||
|
Reference in New Issue
Block a user