Skip to content

Commit

Permalink
More startup checks
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebanana420 committed Jul 24, 2024
1 parent 8acb131 commit 4b379c3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
9 changes: 5 additions & 4 deletions examples/example.tofu
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 0 additions & 11 deletions src/parser.scala → src/parser/parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions src/parser/script_verify.scala
Original file line number Diff line number Diff line change
@@ -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!")
11 changes: 4 additions & 7 deletions src/runner/conditions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/runner/runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/variables/variables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4b379c3

Please sign in to comment.