diff --git a/examples/example.tofu b/examples/example.tofu index 2ea54b6..981d4e2 100644 --- a/examples/example.tofu +++ b/examples/example.tofu @@ -7,10 +7,11 @@ end // this is a comment print $doremy print $0 -set doremy, 45 -set ffargs, scale=iw/2:ih/2 -set doremy2, $doremy -set test, a b c +int doremy, 45 +string ffargs, scale=iw/2:ih/2 +int doremy2, $doremy + +string test, a b c print $test exec ffplay -loglevel 0 "/home/space/Pictures/Kirby Shook_square20x.png" diff --git a/src/parser.scala b/src/parser/parser.scala similarity index 81% rename from src/parser.scala rename to src/parser/parser.scala index 5e52df2..3b52f20 100644 --- a/src/parser.scala +++ b/src/parser/parser.scala @@ -42,17 +42,6 @@ def getFuncNames(script: Seq[String], indexes: Seq[Int], names: Vector[String] = val name = getName(line, name_start) getFuncNames(script, indexes, names :+ name, i+1) -private def verifyCode(script: Seq[String], start_keyword: String, end_keyword: String, start_count: Int = 0, end_count: Int = 0, i: Int = 0): Boolean = - if i >= script.length then start_count == end_count - else - val more_start = if startsWith_strict(script(i), start_keyword) then 1 else 0 - val more_end = if startsWith_strict(script(i), end_keyword) then 1 else 0 - verifyCode(script, start_keyword, end_keyword, start_count + more_start, end_count + more_end, i+1) - -def verifyFunctions(script: Seq[String]): Boolean = verifyCode(script, "function", "end") -def verifyIfs(script: Seq[String]): Boolean = verifyCode(script, "if", "endif") -def verifyWhile(script: Seq[String]): Boolean = verifyCode(script, "while", "endwhile") - def mkstr_raw(in: Seq[String], str: String = "", i: Int = 0): String = if i >= in.length then str else if i == in.length-1 then mkstr_raw(in, str + s"${in(i)}", i+1) diff --git a/src/parser/script_verify.scala b/src/parser/script_verify.scala new file mode 100644 index 0000000..92d51f2 --- /dev/null +++ b/src/parser/script_verify.scala @@ -0,0 +1,33 @@ +package tofu.parser + +import tofu.reader.findLineStart +import tofu.variables.{readVariable_str_safe, readVariable_int_safe} +import tofu.{debugMessage, debug_printSeq, closeTofu} +import tofu.runner.conditionElements +import tofu.variables.getVariableProperties + +private def verifyCode(script: Seq[String], start_keyword: String, end_keyword: String, start_count: Int = 0, end_count: Int = 0, i: Int = 0): Boolean = + if i >= script.length then start_count == end_count + else + val more_start = if startsWith_strict(script(i), start_keyword) then 1 else 0 + val more_end = if startsWith_strict(script(i), end_keyword) then 1 else 0 + verifyCode(script, start_keyword, end_keyword, start_count + more_start, end_count + more_end, i+1) + +def verifyFunctions(script: Seq[String]): Boolean = verifyCode(script, "function", "end") +def verifyIfs(script: Seq[String]): Boolean = verifyCode(script, "if", "endif") +def verifyWhile(script: Seq[String]): Boolean = verifyCode(script, "while", "endwhile") + +private def verifyCondition(line: String, isIF: Boolean): Boolean = + val start = if isIF then findLineStart(line, 2) else findLineStart(line, 5) //for while loops + if start == -1 || conditionElements(line, start).length == 0 then false + else true + +def verifyConditions(script: Seq[String]) = + for s <- script do + if startsWith_strict(s, "if") && !verifyCondition(s, true) then + closeTofu(s"Syntax error! If statement condition at line\n$s\nLacks elements and an operator to compare them to!") + +def verifyWhileCondition(script: Seq[String]) = + for s <- script do + if startsWith_strict(s, "while") && !verifyCondition(s, false) then + closeTofu(s"Syntax error! While loop condition at line\n$s\nLacks elements and an operator to compare them to!") diff --git a/src/runner/conditions.scala b/src/runner/conditions.scala index 7e2c468..1395b62 100644 --- a/src/runner/conditions.scala +++ b/src/runner/conditions.scala @@ -27,9 +27,6 @@ def checkCondition(line: String, isIF: Boolean): Boolean = val start = if isIF then findLineStart(line, 2) else findLineStart(line, 5) //for while loops val elements = conditionElements(line, start) elements.length match - case 0 => - closeTofu(s"Syntax error! Condition at line\n$line\nlacks elements and an operator to compare them to!") - false case 1 => debugMessage("If statement has only 1 element, returning true if variable exists or is not a variable") val variable = readVariable_class_safe(elements(0)) @@ -53,10 +50,7 @@ def checkCondition(line: String, isIF: Boolean): Boolean = debugMessage(s"Condition returned $condition") condition -private def addElement(x: Vector[String], y: String): Vector[String] = - if y.length == 0 then x else x:+y - -private def conditionElements(line: String, i: Int, elements: Vector[String] = Vector(), s: String = "", ignore_spaces: Boolean = false): Vector[String] = +def conditionElements(line: String, i: Int, elements: Vector[String] = Vector(), s: String = "", ignore_spaces: Boolean = false): Vector[String] = if i >= line.length || elements.length == 3 then addElement(elements, s) else if line(i) == ' ' || line(i) == '\t' && !ignore_spaces then conditionElements(line, i+1, elements :+ s, "", ignore_spaces) @@ -65,6 +59,9 @@ private def conditionElements(line: String, i: Int, elements: Vector[String] = V else conditionElements(line, i+1, elements, s + line(i), ignore_spaces) +private def addElement(x: Vector[String], y: String): Vector[String] = + if y.length == 0 then x else x:+y + private def compare_str_contains(e0: TofuVar, e1: TofuVar, equals: Boolean): Boolean = val str0 = if e0.vartype == variable_type.none then e0.input diff --git a/src/runner/runner.scala b/src/runner/runner.scala index 34241b0..495545a 100644 --- a/src/runner/runner.scala +++ b/src/runner/runner.scala @@ -142,3 +142,6 @@ def runStartupChecks(script: Seq[String]) = if !verifyFunctions(script) then closeTofu("Syntax error! All functions must be followed by the \"end\" keyword to define where they end!") if !verifyIfs(script) then closeTofu("Syntax error! All if statements must be followed by the \"endif\" keyword to define where they end!") if !verifyWhile(script) then closeTofu("Syntax error! All while loops must be followed by the \"endwhile\" keyword to define where they end!") + + verifyConditions(script) + verifyWhileCondition(script) diff --git a/src/variables/variables.scala b/src/variables/variables.scala index 840e3e5..3eaaa2c 100644 --- a/src/variables/variables.scala +++ b/src/variables/variables.scala @@ -40,7 +40,7 @@ class TofuVar(name: String): private def getValue_int(): Int = int_val(pointer) end TofuVar -private def getVariableProperties(line: String, keyword: String): Vector[String] = +def getVariableProperties(line: String, keyword: String): Vector[String] = val start = findLineStart(line, keyword.length) val name = getName_variable(line, start) val value = findVariableVal(line, start)