Created the Scanner for the interpreter

main
oabrivard 2 years ago
parent c24a76ad14
commit 67985365df

61
.gitignore vendored

@ -1,29 +1,42 @@
.DS_Store .gradle
.idea build/
!gradle/wrapper/gradle-wrapper.jar
# ---> Kotlin !**/src/main/**/build/
# Compiled class file !**/src/test/**/build/
*.class
# Log file ### IntelliJ IDEA ###
*.log .idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
# BlueJ files ### Eclipse ###
*.ctxt .apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
# Mobile Tools for Java (J2ME) ### NetBeans ###
.mtj.tmp/ /nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# Package Files # ### VS Code ###
*.jar .vscode/
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$USER_HOME$/.sdkman/candidates/gradle/8.5" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="2.0.0" />
</component>
</project>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ScalaSbtSettings">
<option name="customVMPath" />
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,33 @@
plugins {
kotlin("jvm") version "2.0.0"
}
group = "fr.celticinfo"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
sourceSets {
main {
java.srcDirs("src/main")
}
test {
java.srcDirs("src/test")
}
}
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
kotlin {
jvmToolchain(21)
}

@ -0,0 +1 @@
kotlin.code.style=official

Binary file not shown.

@ -0,0 +1,6 @@
#Fri Jun 21 04:48:26 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

234
gradlew vendored

@ -0,0 +1,234 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# 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
#
# https://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.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

89
gradlew.bat vendored

@ -0,0 +1,89 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

@ -0,0 +1,5 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}
rootProject.name = "kotlinlox"

@ -0,0 +1,55 @@
package fr.celticinfo.lox
import kotlin.system.exitProcess
fun main(args: Array<String>) {
val lox = Lox()
when (args.size) {
0 -> lox.runPrompt()
1 -> lox.runFile(args.first())
else -> {println("Usage: jlox [script]") ; exitProcess(64)}
}
}
/**
* Lox is the main class that runs the Lox interpreter.
*/
class Lox {
private var hadError = false
fun runPrompt() {
while (true) {
print("> ")
val line = readlnOrNull() ?: break
run(line)
hadError = false;
}
}
fun runFile(filename: String) {
// read all bytes from a file
val bytes = java.io.File(filename).readBytes()
run(String(bytes))
// Indicate an error in the exit code.
if (hadError) exitProcess(65)
}
private fun run(source: String) {
val scanner = Scanner(source)
val tokens = scanner.scanTokens()
for (token in tokens) {
println(token)
}
}
companion object {
fun error(line: Int, s: String) {
report(line, "", s)
}
private fun report(line: Int, where: String, message: String) {
System.err.println("[line $line] Error$where: $message")
}
}
}

@ -0,0 +1,202 @@
package fr.celticinfo.lox
import fr.celticinfo.lox.TokenType.*
/**
* The Scanner class is responsible for scanning the source code and generating tokens.
* It handles different types of tokens including identifiers, numbers, strings, and special characters.
* It also handles comments and ignores whitespaces.
*
* @property source The source code to be scanned.
* @property tokens The list of tokens generated from the source code.
* @property start The starting index of the current lexeme in the source code.
* @property current The current index in the source code.
* @property line The current line number in the source code.
*/
class Scanner {
private val source: String
private val tokens: MutableList<Token> = mutableListOf()
private var start = 0
private var current = 0
private var line = 1
constructor(source: String) {
this.source = source
}
fun scanTokens(): List<Token> {
while (!isAtEnd()) {
// We are at the beginning of the next lexeme.
start = current
scanToken()
}
tokens.add(Token(TokenType.EOF, "", null, line))
return tokens
}
private fun isAtEnd(): Boolean {
return current >= source.length
}
private fun scanToken() {
when (val c = advance()) {
'(' -> addToken(LEFT_PAREN)
')' -> addToken(RIGHT_PAREN)
'{' -> addToken(LEFT_BRACE)
'}' -> addToken(RIGHT_BRACE)
',' -> addToken(COMMA)
'.' -> addToken(DOT)
'-' -> addToken(MINUS)
'+' -> addToken(PLUS)
';' -> addToken(SEMICOLON)
'*' -> addToken(STAR)
'!' -> addToken(if (match('=')) BANG_EQUAL else BANG)
'=' -> addToken(if (match('=')) EQUAL_EQUAL else EQUAL)
'<' -> addToken(if (match('=')) LESS_EQUAL else LESS)
'>' -> addToken(if (match('=')) GREATER_EQUAL else GREATER)
' ', '\r', '\t' -> {/* Ignore whitespace. */ }
'"' -> string()
'\n' -> line++
'/' -> when {
match('/') -> eatLineComment()
match('*') -> eatBlockComment()
else -> addToken(SLASH)
}
else -> {
when {
c.isDigit() -> number()
c.isLetter() -> identifier()
else -> Lox.error(line, "Unexpected character.")
}
}
}
}
private fun identifier() {
while (peek().isLetterOrDigit()) advance()
// See if the identifier is a reserved word.
val text = source.substring(start, current)
val type = keywords[text] ?: IDENTIFIER
addToken(type)
}
private fun number() {
while (peek().isDigit()) advance()
// Look for a fractional part.
if (peek() == '.' && peekNext().isDigit()) {
// Consume the "."
advance()
while (peek().isDigit()) advance()
}
addToken(NUMBER, source.substring(start, current).toDouble())
}
private fun peekNext(): Char {
if (current + 1 >= source.length) return '\u0000'
return source[current + 1]
}
private fun string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++
advance()
}
// Unterminated string.
if (isAtEnd()) {
Lox.error(line, "Unterminated string.")
return
}
// The closing ".
advance()
// Trim the surrounding quotes.
val value = source.substring(start + 1, current - 1)
addToken(STRING, value)
}
private fun advance(): Char {
current++
return source[current - 1]
}
private fun addToken(type: TokenType, literal: Any? = null) {
val text = source.substring(start, current)
tokens.add(Token(type, text, literal, line))
}
private fun match(expected: Char): Boolean {
if (isAtEnd()) return false
if (source[current] != expected) return false
current++
return true
}
private fun peek(): Char {
if (isAtEnd()) return '\u0000'
return source[current]
}
private fun eatLineComment() {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd()) advance()
}
private fun eatBlockComment() {
var nesting = 1
while (nesting > 0 && !isAtEnd()) {
when {
peek() == '/' && peekNext() == '*' -> {
advance()
advance()
nesting++
}
peek() == '*' && peekNext() == '/' -> {
advance()
advance()
nesting--
}
peek() == '\n' -> {
line++
advance()
}
else -> advance()
}
}
if (nesting > 0) {
Lox.error(line, "Unterminated block comment.")
}
}
companion object {
private val keywords = mapOf(
"and" to AND,
"class" to CLASS,
"else" to ELSE,
"false" to FALSE,
"for" to FOR,
"fun" to FUN,
"if" to IF,
"nil" to NIL,
"or" to OR,
"print" to PRINT,
"return" to RETURN,
"super" to SUPER,
"this" to THIS,
"true" to TRUE,
"var" to VAR,
"while" to WHILE
)
}
}

@ -0,0 +1,16 @@
package fr.celticinfo.lox
/**
* The Token class represents a token in the Lox language. A token is a meaningful unit of code,
* such as a keyword, identifier, operator, or literal value.
*
* @property type The type of the token, represented as a TokenType.
* @property lexeme The raw text of the token as it appears in the source code.
* @property literal The literal value of the token, if it has one. For example, the literal value of a number token is the numeric value it represents.
* @property line The line number in the source code where the token appears.
*/
data class Token(val type: TokenType, val lexeme: String, val literal: Any?, val line: Int) {
override fun toString(): String {
return "$type $lexeme $literal $line"
}
}

@ -0,0 +1,27 @@
package fr.celticinfo.lox
/**
* The TokenType enum represents the different types of tokens that can be recognized by the Scanner.
* This includes various keywords, operators, literals, and identifiers in the Lox language.
*/
enum class TokenType {
// Single-character tokens.
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR,
// One or two character tokens.
BANG, BANG_EQUAL,
EQUAL, EQUAL_EQUAL,
GREATER, GREATER_EQUAL,
LESS, LESS_EQUAL,
// Literals.
IDENTIFIER, STRING, NUMBER,
// Keywords.
AND, CLASS, ELSE, FALSE, FUN, FOR, IF, NIL, OR,
PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE,
// End of file.
EOF
}

@ -0,0 +1,203 @@
package fr.celticinfo.lox
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import fr.celticinfo.lox.TokenType.*
class ScannerTest {
@Test
fun `validate scanner`() {
val code = """
// Your first Lox program!
print "Hello, world!";
true; // Not false.
false; // Not *not* false.
1234; // An integer.
12.34; // A decimal number.
"I am a string";
""; // The empty string.
"123"; // This is a string, not a number.
+ / * -
< <= > >= == !=
!true and or
var average = (min + max) / 2;
{
print "Two statements.";
}
if (condition)
{
} else
{
}
while (a < 10)
{
a = a + 1;
}
for (var a = 1; a < 10; a = a + 1)
{
print a;
}
makeBreakfast(bacon, eggs, toast);
fun returnSum(a, b)
{
return a + b;
}
class Breakfast
{
cook()
{
print "Eggs a-fryin'!";
}
}
breakfast.meat = "sausage";
/* coucou
/* hi
hi */
*/
""".trimIndent()
val scanner = Scanner(code)
val expected = listOf(
Token(PRINT, "print", null, 2),
Token(STRING, "\"Hello, world!\"", "Hello, world!", 2),
Token(SEMICOLON, ";", null, 2),
Token(TRUE, "true", null, 3),
Token(SEMICOLON, ";", null, 3),
Token(FALSE, "false", null, 4),
Token(SEMICOLON, ";", null, 4),
Token(NUMBER, "1234", 1234.0, 5),
Token(SEMICOLON, ";", null, 5),
Token(NUMBER, "12.34", 12.34, 6),
Token(SEMICOLON, ";", null, 6),
Token(STRING, "\"I am a string\"", "I am a string", 7),
Token(SEMICOLON, ";", null, 7),
Token(STRING, "\"\"", "", 8),
Token(SEMICOLON, ";", null, 8),
Token(STRING, "\"123\"", "123", 9),
Token(SEMICOLON, ";", null, 9),
Token(PLUS, "+", null, 10),
Token(SLASH, "/", null, 10),
Token(STAR, "*", null, 10),
Token(MINUS, "-", null, 10),
Token(LESS, "<", null, 11),
Token(LESS_EQUAL, "<=", null, 11),
Token(GREATER, ">", null, 11),
Token(GREATER_EQUAL, ">=", null, 11),
Token(EQUAL_EQUAL, "==", null, 11),
Token(BANG_EQUAL, "!=", null, 11),
Token(BANG, "!", null, 12),
Token(TRUE, "true", null, 12),
Token(AND, "and", null, 12),
Token(OR, "or", null, 12),
Token(VAR, "var", null, 13),
Token(IDENTIFIER, "average", null, 13),
Token(EQUAL, "=", null, 13),
Token(LEFT_PAREN, "(", null, 13),
Token(IDENTIFIER, "min", null, 13),
Token(PLUS, "+", null, 13),
Token(IDENTIFIER, "max", null, 13),
Token(RIGHT_PAREN, ")", null, 13),
Token(SLASH, "/", null, 13),
Token(NUMBER, "2", 2.0, 13),
Token(SEMICOLON, ";", null, 13),
Token(LEFT_BRACE, "{", null, 14),
Token(PRINT, "print", null, 15),
Token(STRING, "\"Two statements.\"", "Two statements.", 15),
Token(SEMICOLON, ";", null, 15),
Token(RIGHT_BRACE, "}", null, 16),
Token(IF, "if", null, 17),
Token(LEFT_PAREN, "(", null, 17),
Token(IDENTIFIER, "condition", null, 17),
Token(RIGHT_PAREN, ")", null, 17),
Token(LEFT_BRACE, "{", null, 18),
Token(RIGHT_BRACE, "}", null, 19),
Token(ELSE, "else", null, 19),
Token(LEFT_BRACE, "{", null, 20),
Token(RIGHT_BRACE, "}", null, 21),
Token(WHILE, "while", null, 22),
Token(LEFT_PAREN, "(", null, 22),
Token(IDENTIFIER, "a", null, 22),
Token(LESS, "<", null, 22),
Token(NUMBER, "10", 10.0, 22),
Token(RIGHT_PAREN, ")", null, 22),
Token(LEFT_BRACE, "{", null, 23),
Token(IDENTIFIER, "a", null, 24),
Token(EQUAL, "=", null, 24),
Token(IDENTIFIER, "a", null, 24),
Token(PLUS, "+", null, 24),
Token(NUMBER, "1", 1.0, 24),
Token(SEMICOLON, ";", null, 24),
Token(RIGHT_BRACE, "}", null, 25),
Token(FOR, "for", null, 26),
Token(LEFT_PAREN, "(", null, 26),
Token(VAR, "var", null, 26),
Token(IDENTIFIER, "a", null, 26),
Token(EQUAL, "=", null, 26),
Token(NUMBER, "1", 1.0, 26),
Token(SEMICOLON, ";", null, 26),
Token(IDENTIFIER, "a", null, 26),
Token(LESS, "<", null, 26),
Token(NUMBER, "10", 10.0, 26),
Token(SEMICOLON, ";", null, 26),
Token(IDENTIFIER, "a", null, 26),
Token(EQUAL, "=", null, 26),
Token(IDENTIFIER, "a", null, 26),
Token(PLUS, "+", null, 26),
Token(NUMBER, "1", 1.0, 26),
Token(RIGHT_PAREN, ")", null, 26),
Token(LEFT_BRACE, "{", null, 27),
Token(PRINT, "print", null, 28),
Token(IDENTIFIER, "a", null, 28),
Token(SEMICOLON, ";", null, 28),
Token(RIGHT_BRACE, "}", null, 29),
Token(IDENTIFIER, "makeBreakfast", null, 30),
Token(LEFT_PAREN, "(", null, 30),
Token(IDENTIFIER, "bacon", null, 30),
Token(COMMA, ",", null, 30),
Token(IDENTIFIER, "eggs", null, 30),
Token(COMMA, ",", null, 30),
Token(IDENTIFIER, "toast", null, 30),
Token(RIGHT_PAREN, ")", null, 30),
Token(SEMICOLON, ";", null, 30),
Token(FUN, "fun", null, 31),
Token(IDENTIFIER, "returnSum", null, 31),
Token(LEFT_PAREN, "(", null, 31),
Token(IDENTIFIER, "a", null, 31),
Token(COMMA, ",", null, 31),
Token(IDENTIFIER, "b", null, 31),
Token(RIGHT_PAREN, ")", null, 31),
Token(LEFT_BRACE, "{", null, 32),
Token(RETURN, "return", null, 33),
Token(IDENTIFIER, "a", null, 33),
Token(PLUS, "+", null, 33),
Token(IDENTIFIER, "b", null, 33),
Token(SEMICOLON, ";", null, 33),
Token(RIGHT_BRACE, "}", null, 34),
Token(CLASS, "class", null, 35),
Token(IDENTIFIER, "Breakfast", null, 35),
Token(LEFT_BRACE, "{", null, 36),
Token(IDENTIFIER, "cook", null, 37),
Token(LEFT_PAREN, "(", null, 37),
Token(RIGHT_PAREN, ")", null, 37),
Token(LEFT_BRACE, "{", null, 38),
Token(PRINT, "print", null, 39),
Token(STRING, "\"Eggs a-fryin'!\"", "Eggs a-fryin'!", 39),
Token(SEMICOLON, ";", null, 39),
Token(RIGHT_BRACE, "}", null, 40),
Token(RIGHT_BRACE, "}", null, 41),
Token(IDENTIFIER, "breakfast", null, 42),
Token(DOT, ".", null, 42),
Token(IDENTIFIER, "meat", null, 42),
Token(EQUAL, "=", null, 42),
Token(STRING, "\"sausage\"", "sausage", 42),
Token(SEMICOLON, ";", null, 42),
Token(EOF, "", null, 47))
val tokens = scanner.scanTokens()
expected.zip(tokens).forEach { (expected, actual) ->
assertEquals(expected, actual, "Expected $expected but got $actual")
}
}
}

@ -0,0 +1,16 @@
package fr.celticinfo.lox
import org.junit.jupiter.api.Test
import kotlin.test.*
class TokenTest {
@Test
fun testToString() {
val token = Token(TokenType.IDENTIFIER, "foo", null, 1)
assertEquals("IDENTIFIER foo null 1", token.toString())
val token2 = Token(TokenType.STRING, "bar", "baz", 2)
assertEquals("STRING bar baz 2", token2.toString())
}
}

@ -0,0 +1,56 @@
package fr.celticinfo.lox
import org.junit.jupiter.api.Test
import kotlin.test.*
class TokenTypeTest {
@Test
fun `TokenType contains expected values`() {
assertTrue(TokenType.values().contains(TokenType.LEFT_PAREN))
assertTrue(TokenType.values().contains(TokenType.RIGHT_PAREN))
assertTrue(TokenType.values().contains(TokenType.LEFT_BRACE))
assertTrue(TokenType.values().contains(TokenType.RIGHT_BRACE))
assertTrue(TokenType.values().contains(TokenType.COMMA))
assertTrue(TokenType.values().contains(TokenType.DOT))
assertTrue(TokenType.values().contains(TokenType.MINUS))
assertTrue(TokenType.values().contains(TokenType.PLUS))
assertTrue(TokenType.values().contains(TokenType.SEMICOLON))
assertTrue(TokenType.values().contains(TokenType.SLASH))
assertTrue(TokenType.values().contains(TokenType.STAR))
assertTrue(TokenType.values().contains(TokenType.BANG))
assertTrue(TokenType.values().contains(TokenType.BANG_EQUAL))
assertTrue(TokenType.values().contains(TokenType.EQUAL))
assertTrue(TokenType.values().contains(TokenType.EQUAL_EQUAL))
assertTrue(TokenType.values().contains(TokenType.GREATER))
assertTrue(TokenType.values().contains(TokenType.GREATER_EQUAL))
assertTrue(TokenType.values().contains(TokenType.LESS))
assertTrue(TokenType.values().contains(TokenType.LESS_EQUAL))
assertTrue(TokenType.values().contains(TokenType.IDENTIFIER))
assertTrue(TokenType.values().contains(TokenType.STRING))
assertTrue(TokenType.values().contains(TokenType.NUMBER))
assertTrue(TokenType.values().contains(TokenType.AND))
assertTrue(TokenType.values().contains(TokenType.CLASS))
assertTrue(TokenType.values().contains(TokenType.ELSE))
assertTrue(TokenType.values().contains(TokenType.FALSE))
assertTrue(TokenType.values().contains(TokenType.FUN))
assertTrue(TokenType.values().contains(TokenType.FOR))
assertTrue(TokenType.values().contains(TokenType.IF))
assertTrue(TokenType.values().contains(TokenType.NIL))
assertTrue(TokenType.values().contains(TokenType.OR))
assertTrue(TokenType.values().contains(TokenType.PRINT))
assertTrue(TokenType.values().contains(TokenType.RETURN))
assertTrue(TokenType.values().contains(TokenType.SUPER))
assertTrue(TokenType.values().contains(TokenType.THIS))
assertTrue(TokenType.values().contains(TokenType.TRUE))
assertTrue(TokenType.values().contains(TokenType.VAR))
assertTrue(TokenType.values().contains(TokenType.WHILE))
assertTrue(TokenType.values().contains(TokenType.EOF))
assertEquals(39, TokenType.values().size)
}
}
Loading…
Cancel
Save