Permalink
Browse files

Added ability to set functions through the def keyword (check sources…

… for more details)
  • Loading branch information...
1 parent cd9308e commit 31c7f088a9b37b9db81f7703ab5adaff9fd429fc @shamanas shamanas committed Dec 11, 2010
Showing with 107 additions and 4 deletions.
  1. +6 −0 system.thtml
  2. +66 −2 templateLoader.ooc
  3. +35 −2 tokenizer.ooc
View
@@ -0,0 +1,6 @@
+<%
+def[ExecuteFile:file] {
+ ReadFile: data,file
+ Execute: data
+}
+%>
View
@@ -2,12 +2,12 @@ use oocsv
use oocgi
import oocsv
import oocgi
-import structs/MultiMap
import io/File
-import structs/ArrayList
+import structs/[ArrayList,MultiMap]
import addressParser
import tokenizer
import os/Time
+import text/StringTokenizer
// NO SEGFAULTS, BUT MANY THINGS TO FIX :/
@@ -17,7 +17,33 @@ Function : class
callback : Func(ArrayList<String>,TemplateLoader) -> String
init : func (=name,=callback)
+}
+
+UserFunction : class extends Function
+{
+ code := ArrayList<Token> new()
+ funcArgs := ArrayList<String> new()
+ init : func ~extended (=code,=funcArgs,.name)
{
+ super(name,func(args : ArrayList<String>, tl : TemplateLoader)
+ {
+ if(args size == funcArgs size)
+ {
+ temp := tl thtmlVars
+ for(i in 0 .. funcArgs size)
+ {
+ tl thtmlVars[funcArgs get(i)] = tl resolveVariable(args get(i))
+ }
+ ret := Tokenizer execute(code,tl)
+ tl thtmlVars = temp
+ return ret
+ }
+ else
+ {
+ return "Function " + name + " takes " + funcArgs size toString() + " arguments."
+ }
+ ""
+ })
}
}
@@ -363,6 +389,21 @@ TemplateLoader : class // class that takes care of loading the template's databa
""
}))
+ thtmlFunctions add(Function new("ArrayCount",func(args : ArrayList<String>, tl : TemplateLoader) {
+ if(args size == 2)
+ {
+ if(tl thtmlArrays != null && tl thtmlArrays get(args get(1)) != null)//if there is an array with that name
+ {
+ tl thtmlVars[args get(0)] = tl thtmlArrays get(args get(1)) array size toString()
+ }
+ }
+ else
+ {
+ return "ERROR: Function ArrayCount takes two arguments."
+ }
+ ""
+ }))
+
thtmlFunctions add(Function new("LineCount",func(args : ArrayList<String>, tl : TemplateLoader) {
if(args size == 1)
{
@@ -652,6 +693,7 @@ TemplateLoader : class // class that takes care of loading the template's databa
getDesign()
+ parseFile("system.thtml")
replaceOpens := base findAll("__[")
replaceCloses := base findAll("]__")
if(replaceOpens size == replaceCloses size)
@@ -790,6 +832,28 @@ TemplateLoader : class // class that takes care of loading the template's databa
return null // No variable found, send back null ;(
}
+ makeFunction : func (decl : String, tokens : ArrayList<Token>)
+ {
+ if(decl[0] == '[' && decl[decl size-1] == ']')
+ {
+ decl = decl substring(1,decl size-1)
+ parts := decl split(':')
+ if(parts size == 2)
+ {
+ name := parts get(0)
+ argsDecl := parts get(1) replaceAll(" ","")
+ moreParts := argsDecl split(',')
+ funcArgs := ArrayList<String> new()
+ for(part in moreParts)
+ {
+ funcArgs add(part)
+ }
+ function := UserFunction new(tokens,funcArgs,name)
+ thtmlFunctions add(function)
+ }
+ }
+ }
+
parseCondition : func (cond : String) -> Bool
{
if(cond[0] != '[' || cond[cond size-1] != ']')
View
@@ -80,16 +80,20 @@ Tokenizer : class
if(getLeftToken(tokens,i) != -1)
{
left := getLeftToken(tokens,i)
+ mergeTokens(tokens,i,findRightToken(tokens,i,"]"))
if(tokens get(left) value == "if")
{
- mergeTokens(tokens,i,findRightToken(tokens,i,"]"))
tokens get(i) type = "condition"
}
else if(tokens get(left) value == "for")
{
- mergeTokens(tokens,i,findRightToken(tokens,i,"]"))
+
tokens get(i) type = "loop"
}
+ else if(tokens get(left) value == "def")
+ {
+ tokens get(i) type = "funcDecl"
+ }
}
}
else if(tokens get(i) value == "\"")
@@ -337,6 +341,35 @@ Tokenizer : class
return "Error: expected a loop after for keyword. (Did you forget '[' or ']' ?) , ( Found " + tokens get(loop) value + " at the place of a loop )"
}
}
+ else if(tokens get(i) value == "def")
+ {
+ decl := getRightToken(tokens,i)
+ if(tokens get(decl) type == "funcDecl")
+ {
+ openIndx := getRightToken(tokens,decl)
+ if(tokens get(openIndx) value == "{")
+ {
+ closeIndx := findNested(tokens,openIndx,"{","}")
+ if(closeIndx != -1)
+ {
+ tl makeFunction(tokens get(decl) value, tokens slice(openIndx+1 .. closeIndx-1))
+ tokens = deleteTokens(tokens,i,closeIndx)
+ }
+ else
+ {
+ return "Error: block not closed. (Did you forget '}' ?)"
+ }
+ }
+ else
+ {
+ return "Error: expected a block after for keyword. Found " + tokens get(openIndx) value + " in the place of '{'"
+ }
+ }
+ else
+ {
+ return "Error: expected a function declaration after for keyword. (Did you forget '[' or ']' ?) , ( Found " + tokens get(decl) value + " at the place of a function declaration )"
+ }
+ }
}
// Final phase, just merge everything =)
tokens = mergeTokens(tokens,0,tokens size-1)

0 comments on commit 31c7f08

Please sign in to comment.