Compare commits

...

12 Commits

13 changed files with 92 additions and 36 deletions
+22 -3
View File
@@ -135,6 +135,9 @@ func main() {
}
----
=== Concepts and terminology
#TODO#
=== Data types
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
@@ -260,7 +263,15 @@ _Expr_ supports list of mixed-type values, also specified by normal expressions.
|===
=== Variables
#TODO: variables#
A variable is an identifier with an assigned value. Variables are stored in the object that implements the _ExprContext_ interface.
.Examples
[source,go]
----
a=1
x = 5.2 * (9-3)
x = 1; y = 2*x
----
=== Other operations
@@ -346,12 +357,15 @@ The table below shows all supported operators by decreasing priorities.
.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ "and" _boolean_ -> _boolean_
| [blue]`&&` | _Infix_ | _and_ | _boolean_ "&&" _boolean_ -> _boolean_
.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ "or" _boolean_ -> _boolean_
| [blue]`||` | _Infix_ | _or_ | _boolean_ "||" _boolean_ -> _boolean_
| [blue]`\|\|` | _Infix_ | _or_ | _boolean_ "\|\|" _boolean_ -> _boolean_
.1+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_
.1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_
|===
=== Functions
Functions in _Expr_ are very similar to functions in many programming languages.
In _Expr_ functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator [blue]`@` it is possibile to export local definition to the calling context.
==== Function calls
#TODO: function calls operations#
@@ -359,8 +373,13 @@ The table below shows all supported operators by decreasing priorities.
==== Function definitions
#TODO: function definitions operations#
==== Builtins
=== Builtins
#TODO: builtins#
==== Builtin functions
==== [blue]_import()_
[blue]_import([grey]#<source-file>#)_ loads the multi-expression contained in the specified source and returns its value.
+2 -1
View File
@@ -12,6 +12,7 @@ import (
type Expr interface {
Eval(ctx ExprContext) (result any, err error)
eval(ctx ExprContext, preset bool) (result any, err error)
String() string
}
//-------- ast
@@ -118,7 +119,7 @@ func (self *ast) eval(ctx ExprContext, preset bool) (result any, err error) {
if self.forest != nil {
for _, root := range self.forest {
if result, err = root.compute(ctx); err == nil {
ctx.setVar(control_last_result, result)
ctx.setVar(ControlLastResult, result)
} else {
//err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
break
+9 -9
View File
@@ -5,9 +5,9 @@ import "strings"
// Preset control variables
const (
control_last_result = "_last"
control_bool_shortcut = "_bool_shortcut"
control_import_path = "_import_path"
ControlLastResult = "last"
ControlBoolShortcut = "_bool_shortcut"
ControlImportPath = "_import_path"
)
// Other control variables
@@ -21,23 +21,23 @@ const (
)
func initDefaultVars(ctx ExprContext) {
ctx.setVar(control_bool_shortcut, true)
ctx.setVar(control_import_path, init_import_path)
ctx.SetVar(ControlBoolShortcut, true)
ctx.SetVar(ControlImportPath, init_import_path)
}
func enable(ctx ExprContext, name string) {
if strings.HasPrefix(name, "_") {
ctx.setVar(name, true)
ctx.SetVar(name, true)
} else {
ctx.setVar("_"+name, true)
ctx.SetVar("_"+name, true)
}
}
func disable(ctx ExprContext, name string) {
if strings.HasPrefix(name, "_") {
ctx.setVar(name, false)
ctx.SetVar(name, false)
} else {
ctx.setVar("_"+name, false)
ctx.SetVar("_"+name, false)
}
}
+2 -2
View File
@@ -51,7 +51,7 @@ func addEnvImportDirs(dirList []string) []string {
}
func addPresetImportDirs(ctx ExprContext, dirList []string) []string {
if dirSpec, exists := getControlString(ctx, control_import_path); exists {
if dirSpec, exists := getControlString(ctx, ControlImportPath); exists {
dirs := strings.Split(dirSpec, ":")
if dirList == nil {
dirList = dirs
@@ -133,7 +133,7 @@ func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (resu
return
}
func importImportFunc(ctx ExprContext) {
func ImportImportFunc(ctx ExprContext) {
ctx.RegisterFunc("import", &simpleFunctor{f: importFunc}, 1, -1)
ctx.RegisterFunc("include", &simpleFunctor{f: includeFunc}, 1, -1)
}
+1 -1
View File
@@ -101,7 +101,7 @@ func mulFunc(ctx ExprContext, name string, args []any) (result any, err error) {
return
}
func importMathFuncs(ctx ExprContext) {
func ImportMathFuncs(ctx ExprContext) {
ctx.RegisterFunc("add", &simpleFunctor{f: addFunc}, 0, -1)
ctx.RegisterFunc("mul", &simpleFunctor{f: mulFunc}, 0, -1)
}
-2
View File
@@ -45,7 +45,6 @@ func TestEvalStringA(t *testing.T) {
t.Errorf("Source %q got %v, want %v", source, gotResult, wantResult)
t.Errorf("Error: %v", gotErr)
}
fmt.Println("Hello World!")
}
func TestEvalString(t *testing.T) {
@@ -76,5 +75,4 @@ func TestEvalString(t *testing.T) {
t.Errorf("Source %q got %v, want %v", source, gotResult, wantResult)
t.Errorf("Error: %v", gotErr)
}
fmt.Println("Hello World!")
}
+16 -1
View File
@@ -4,7 +4,10 @@
// operand-selector-case.go
package expr
import "fmt"
import (
"fmt"
"strings"
)
// -------- selector case term
@@ -13,6 +16,18 @@ type selectorCase struct {
caseExpr Expr
}
func (sc *selectorCase) String() string {
var sb strings.Builder
if sc.filterList != nil {
sc.filterList.toString(&sb)
sb.WriteByte(' ')
}
sb.WriteByte('{')
sb.WriteString(sc.caseExpr.String())
sb.WriteByte('}')
return sb.String()
}
func newSelectorCaseTerm(row, col int, filterList *term, caseExpr Expr) *term {
tk := NewValueToken(row, col, SymSelectorCase, "", &selectorCase{filterList: filterList, caseExpr: caseExpr})
return &term{
+2 -2
View File
@@ -48,7 +48,7 @@ func newAndTerm(tk *Token) (inst *term) {
}
func evalAnd(ctx ExprContext, self *term) (v any, err error) {
if isEnabled(ctx, control_bool_shortcut) {
if isEnabled(ctx, ControlBoolShortcut) {
v, err = evalAndWithShortcut(ctx, self)
} else {
v, err = evalAndWithoutShortcut(ctx, self)
@@ -117,7 +117,7 @@ func newOrTerm(tk *Token) (inst *term) {
}
func evalOr(ctx ExprContext, self *term) (v any, err error) {
if isEnabled(ctx, control_bool_shortcut) {
if isEnabled(ctx, ControlBoolShortcut) {
v, err = evalOrWithShortcut(ctx, self)
} else {
v, err = evalOrWithoutShortcut(ctx, self)
+1 -1
View File
@@ -268,7 +268,7 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef
currentTerm, err = tree.addToken2(tk)
}
if currentTerm != nil && currentTerm.parent != nil && currentTerm.parent.tk.Sym != SymSelector {
if currentTerm != nil && currentTerm.tk.Sym != SymSelector && currentTerm.parent != nil && currentTerm.parent.tk.Sym != SymSelector {
selectorTerm = nil
}
+4 -4
View File
@@ -173,8 +173,8 @@ func TestParser(t *testing.T) {
ctx := NewSimpleFuncStore()
ctx.SetVar("var1", int64(123))
ctx.SetVar("var2", "abc")
importMathFuncs(ctx)
importImportFunc(ctx)
ImportMathFuncs(ctx)
ImportImportFunc(ctx)
parser := NewParser(ctx)
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
@@ -204,7 +204,7 @@ func TestParser(t *testing.T) {
} else {
failed++
if i+1 == check_env_expr_path {
t.Logf(`NOTICE: Test nr %d requires EXPR_PATH environment variable with value "."`, check_env_expr_path)
t.Logf(`NOTICE: Test nr %d requires EXPR_PATH="."`, check_env_expr_path)
}
}
}
@@ -249,7 +249,7 @@ func TestListParser(t *testing.T) {
ctx := NewSimpleFuncStore()
ctx.SetVar("var1", int64(123))
ctx.SetVar("var2", "abc")
importMathFuncs(ctx)
ImportMathFuncs(ctx)
parser := NewParser(ctx)
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
+3 -4
View File
@@ -5,7 +5,6 @@
package expr
import (
"fmt"
"testing"
)
@@ -16,7 +15,7 @@ func TestString(t *testing.T) {
tree := NewAst()
if gotErr := tree.addTokens(tk1, tk2, tk3); gotErr == nil {
fmt.Println("Tree:", tree)
t.Log("Tree:", tree)
} else {
t.Errorf("err: got <%v>, want <nil>", gotErr)
}
@@ -27,7 +26,7 @@ func TestGetRoom(t *testing.T) {
tree := NewAst()
if gotErr := tree.addTokens(tk1); gotErr == nil {
fmt.Println("Tree-root room:", tree.root.getRoom())
t.Log("Tree-root room:", tree.root.getRoom())
} else {
t.Errorf("err: got <%v>, want <nil>", gotErr)
}
@@ -37,7 +36,7 @@ func TestGetChildrenCount(t *testing.T) {
tree := NewAst()
if gotErr := tree.addTokens(tk1); gotErr == nil {
fmt.Println("Tree-root children count:", tree.root.getChildrenCount())
t.Log("Tree-root children count:", tree.root.getChildrenCount())
} else {
t.Errorf("err: got <%v>, want <nil>", gotErr)
}
+5 -1
View File
@@ -27,7 +27,11 @@ func (tk *Token) DevString() string {
func (tk *Token) String() string {
if tk.Value != nil {
return fmt.Sprintf("%#v", tk.Value)
if s, ok := tk.Value.(string); ok {
return fmt.Sprintf("%q", s)
} else {
return fmt.Sprintf("%v", tk.Value)
}
}
return fmt.Sprintf("%s", tk.source)
}
+25 -5
View File
@@ -5,14 +5,34 @@
package expr
import (
"fmt"
"testing"
)
func TestDevString(t *testing.T) {
tk1 := NewValueToken(0, 0, SymInteger, "100", 100)
tk2 := NewToken(0, 0, SymPlus, "+")
type inputType struct {
source string
sym Symbol
value any
wantResult string
}
fmt.Println("Token '100':", tk1.DevString())
fmt.Println("Token '+':", tk2.DevString())
inputs := []inputType{
/* 1 */ {"100", SymInteger, 100, `[55]"100"{100}`},
/* 2 */ {"+", SymPlus, nil, `[6]"+"{}`},
}
for i, input := range inputs {
var tk *Token
if input.value == nil {
tk = NewToken(0, 0, input.sym, input.source)
} else {
tk = NewValueToken(0, 0, input.sym, input.source, input.value)
}
t.Logf("Test nr %2d: %q --> %q", i+1, input.source, input.wantResult)
if s := tk.DevString(); s != input.wantResult {
t.Errorf("wrong token from symbol '+': expected %q, got %q", input.wantResult, s)
}
}
}