Add support for line comments.

This commit is contained in:
Alex Zenla 2023-08-21 02:22:43 -07:00
parent 5455846d7a
commit 0b3700667d
Signed by: alex
GPG Key ID: C0780728420EBFE5
4 changed files with 22 additions and 0 deletions

View File

@ -11,6 +11,8 @@ main = { in
multiply = { a, b in
a * b
}
// calculates the result
calculateSimpleResult = calculateSimple()
calculateComplexResult = calculateComplex()
multiplyResult = multiply(50, 50)

View File

@ -197,6 +197,7 @@ class PorkParser(source: PeekableSource<Token>) {
private fun ignoredByParser(type: TokenType): Boolean = when (type) {
TokenType.BlockComment -> true
TokenType.LineComment -> true
TokenType.Whitespace -> true
else -> false
}

View File

@ -75,6 +75,20 @@ class PorkTokenizer(val source: CharSource) {
return Token(TokenType.BlockComment, tokenStart, comment)
}
private fun readLineComment(firstChar: Char): Token {
val comment = buildString {
append(firstChar)
while (true) {
val char = source.peek()
if (char == CharSource.NullChar || char == '\n') {
break
}
append(source.next())
}
}
return Token(TokenType.LineComment, tokenStart, comment)
}
fun next(): Token {
while (source.peek() != CharSource.NullChar) {
tokenStart = source.currentIndex
@ -84,6 +98,10 @@ class PorkTokenizer(val source: CharSource) {
return readBlockComment(char)
}
if (char == '/' && source.peek() == '/') {
return readLineComment(char)
}
for (item in TokenType.SingleChars) {
val itemChar = item.singleChar!!.char
if (itemChar != char) {

View File

@ -27,6 +27,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
Else(Keyword("else")),
Whitespace,
BlockComment,
LineComment,
EndOfFile;
val promotions: List<Promotion> = properties.filterIsInstance<Promotion>()