Compare commits
	
		
			No commits in common. "c977e82d9ec2424680b14749737a0dfde4720f62" and "0bca3333aa699a493c58b7689a3f68b8f2077ba2" have entirely different histories.
		
	
	
		
			c977e82d9e
			...
			0bca3333aa
		
	
		
							
								
								
									
										2
									
								
								ast.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								ast.go
									
									
									
									
									
								
							| @ -63,7 +63,7 @@ func (self *ast) addToken2(tk *Token) (t *term, err error) { | ||||
| 	if t = newTerm(tk, nil); t != nil { | ||||
| 		err = self.addTerm(t) | ||||
| 	} else { | ||||
| 		err = tk.Errorf("unexpected token %q", tk.String()) | ||||
| 		err = tk.Errorf("No term constructor for token %q", tk.String()) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -44,7 +44,7 @@ func TestAddTokensBad(t *testing.T) { | ||||
| func TestAddUnknownTokens(t *testing.T) { | ||||
| 	tk0 := NewToken(0, 0, SymPercent, "%") | ||||
| 
 | ||||
| 	wantErr := errors.New(`unexpected token "%"`) | ||||
| 	wantErr := errors.New(`No term constructor for token "%"`) | ||||
| 
 | ||||
| 	tree := NewAst() | ||||
| 	if gotErr := tree.addToken(tk0); gotErr != nil && gotErr.Error() != wantErr.Error() { | ||||
|  | ||||
| @ -6,7 +6,17 @@ package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	initName    = "init" | ||||
| 	cleanName   = "clean" | ||||
| 	resetName   = "reset" | ||||
| 	nextName    = "next" | ||||
| 	currentName = "current" | ||||
| ) | ||||
| 
 | ||||
| type dataCursor struct { | ||||
| @ -29,24 +39,23 @@ func newDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) { | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| // func mapToString(m map[string]Functor) string {
 | ||||
| // 	var sb strings.Builder
 | ||||
| // 	sb.WriteByte('{')
 | ||||
| // 	for key, _ := range m {
 | ||||
| // 		if sb.Len() > 1 {
 | ||||
| // 			sb.WriteString(fmt.Sprintf(", %q: func(){}", key))
 | ||||
| // 		} else {
 | ||||
| // 			sb.WriteString(fmt.Sprintf("%q: func(){}", key))
 | ||||
| // 		}
 | ||||
| // 	}
 | ||||
| // 	sb.WriteByte('}')
 | ||||
| // 	return sb.String()
 | ||||
| // }
 | ||||
| func mapToString(m map[string]Functor) string { | ||||
| 	var sb strings.Builder | ||||
| 	sb.WriteByte('{') | ||||
| 	for key, _ := range m { | ||||
| 		if sb.Len() > 1 { | ||||
| 			sb.WriteString(fmt.Sprintf(", %q: func(){}", key)) | ||||
| 		} else { | ||||
| 			sb.WriteString(fmt.Sprintf("%q: func(){}", key)) | ||||
| 		} | ||||
| 	} | ||||
| 	sb.WriteByte('}') | ||||
| 	return sb.String() | ||||
| } | ||||
| 
 | ||||
| func (dc *dataCursor) String() string { | ||||
| 	return "$()" | ||||
| 	/* | ||||
| 	   var sb strings.Builder | ||||
| /*	var sb strings.Builder | ||||
| 	sb.WriteString(fmt.Sprintf(`$( | ||||
| 	index: %d, | ||||
| 	ds:    %s, | ||||
| @ -58,34 +67,30 @@ func (dc *dataCursor) String() string { | ||||
| } | ||||
| 
 | ||||
| func (dc *dataCursor) HasOperation(name string) (exists bool) { | ||||
| 	exists = name == indexName | ||||
| 	if !exists { | ||||
| 	f, ok := dc.ds[name] | ||||
| 	exists = ok && isFunctor(f) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (dc *dataCursor) CallOperation(name string, args []any) (value any, err error) { | ||||
| 	if name == indexName { | ||||
| 		value = int64(dc.Index()) | ||||
| 	} else if functor, ok := dc.ds[name]; ok && isFunctor(functor) { | ||||
| 	if functor, ok := dc.ds[name]; ok && isFunctor(functor) { | ||||
| 		if functor == dc.cleanFunc { | ||||
| 			value, err = dc.Clean() | ||||
| 			return nil, dc.Clean() | ||||
| 		} else if functor == dc.resetFunc { | ||||
| 			value, err = dc.Reset() | ||||
| 			return nil, dc.Reset() | ||||
| 		} else { | ||||
| 			ctx := cloneContext(dc.ctx) | ||||
| 			value, err = functor.Invoke(ctx, name, []any{}) | ||||
| 			exportObjects(dc.ctx, ctx) | ||||
| 		} | ||||
| 
 | ||||
| 	} else { | ||||
| 		err = errNoOperation(name) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (dc *dataCursor) Reset() (success bool, err error) { | ||||
| func (dc *dataCursor) Reset() (err error) { | ||||
| 	if dc.resetFunc != nil { | ||||
| 		if dc.resource != nil { | ||||
| 			ctx := cloneContext(dc.ctx) | ||||
| @ -99,11 +104,10 @@ func (dc *dataCursor) Reset() (success bool, err error) { | ||||
| 	} else { | ||||
| 		err = errNoOperation(resetName) | ||||
| 	} | ||||
| 	success = err == nil | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (dc *dataCursor) Clean() (success bool, err error) { | ||||
| func (dc *dataCursor) Clean() (err error) { | ||||
| 	if dc.cleanFunc != nil { | ||||
| 		if dc.resource != nil { | ||||
| 			ctx := cloneContext(dc.ctx) | ||||
| @ -116,7 +120,6 @@ func (dc *dataCursor) Clean() (success bool, err error) { | ||||
| 	} else { | ||||
| 		err = errors.New("no 'clean' function defined in the data-source") | ||||
| 	} | ||||
| 	success = err == nil | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -169,7 +169,7 @@ x = 1; y = 2*x | ||||
| == Other operations | ||||
| 
 | ||||
| === [blue]`;` operator | ||||
| The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The latter is the final result. | ||||
| The semicolon operator [blue]`;` is an infixed operator. It evaluates the left expression first and then the right expression. The latter is the final result. | ||||
| 
 | ||||
| IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions. | ||||
| 
 | ||||
| @ -187,7 +187,7 @@ a=1; b=2; c=3; a+b+c    // returns 6 | ||||
| [blue]`but` is very similar to [blue]`;`. The only difference is that [blue]`;` can't be used inside parenthesis [blue]`(` and [blue]`)`. | ||||
| 
 | ||||
| === Assignment operator [blue]`=` | ||||
| The assignment operator [blue]`=` is used to define variables in the evaluation context or to change their value (see _ExprContext_). | ||||
| The assignment operator [blue]`=` is used to define variable in the evaluation context or to change their value (see _ExprContext_). | ||||
| The value on the left side of [blue]`=` must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation. | ||||
| 
 | ||||
| .Example | ||||
| @ -202,22 +202,13 @@ The _selector operator_ is very similar to the _switch/case/default_ statement a | ||||
| .Syntax | ||||
| [source,bnf] | ||||
| ---- | ||||
| <selector-operator> ::= <select-expression> "?" <selector-case> { ":" <selector-case> } ["::" <default-multi-expression>] | ||||
| <selector-case> ::= [<match-list>] <case-value> | ||||
| <match-list> ::= "["<item>{","<items>}"]" | ||||
| <item> ::= <expression | ||||
| <case-multi-expression> ::= "{" <multi-expression> "}" | ||||
| <multi-expression> ::= <expression> {";" <expression>} | ||||
| <selector-operator> ::= <expression> "?" <selector-case> { ":" <selector-case> } ["::" <case-value>] | ||||
| <selector-case> ::= [<list>] <case-value> | ||||
| <case-value> ::= "{" <multi-expr> "}" | ||||
| <multi-expr> ::= <expr> {";" <expr>} | ||||
| ---- | ||||
| 
 | ||||
| In other words, the selector operator evaluates the expression (`<select-expression>`) on the left-hand side of the `?` symbol; it then compares the result obtained with the values listed in the `<match-list>`'s. If the comparision find a match with a value in a match-list, the associated  `<case-multi-expression>` is evaluted, and its result will be the final result of the selection operation. | ||||
| 
 | ||||
| The match lists are optional. In that case, the position, from left to right, of the `<selector-case>` is used as match-list. Of course, that only works if the select-expression results in an integer. | ||||
| 
 | ||||
| The `:` symbol (colon) is the separator of the selector-cases. Note that if the value of the select-expression does not match any match-list, an error will be issued. Therefore, it is strongly recommended to provide a default (multi-)expression introduced by the `::` symbol (double-colon). Also note that the default expression has no match-list. | ||||
| 
 | ||||
| 
 | ||||
| .Examples | ||||
| .Example | ||||
| [source,go] | ||||
| ---- | ||||
| 1 ? {"a"} : {"b"}                           // returns "b" | ||||
| @ -233,38 +224,33 @@ The `:` symbol (colon) is the separator of the selector-cases. Note that if the | ||||
| The table below shows all supported operators by decreasing priorities. | ||||
| 
 | ||||
| .Operators priorities | ||||
| [cols="^2,^2,^2,^5,^5"] | ||||
| [cols="^2,^2,^2,^5,<5"] | ||||
| |=== | ||||
| | Priority | Operators     | Position | Operation | Operands and results | ||||
| 
 | ||||
| .1+|*ITEM*| [blue]`.` | _Infix_ | _Item_| _collection_ `"."` _key_ -> _any_ | ||||
| .2+|*INC*| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `"++"` -> _integer_ | ||||
| | [blue]`++` | _Postfix_ | _Next item_ | _iterator_ `"++"` -> _any_ | ||||
| .1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ `"!"` -> _integer_ | ||||
| .3+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| (`"+"`\|`"-"`) _number_ -> _number_ | ||||
| | [blue]`#` | _Prefix_ | _Lenght-of_ | `"#"` _collection_ -> _integer_ | ||||
| | [blue]`#` | _Prefix_ | _Size-of_ | `"#"` _iterator_ -> _integer_ | ||||
| .5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ `"*"` _number_ -> _number_ | ||||
| | [blue]`*` | _Infix_ | _String-repeat_ | _string_ `"*"` _integer_ -> _string_ | ||||
| | [blue]`/` | _Infix_ | _Division_ | _number_ `"/"` _number_ -> _number_ | ||||
| | [blue]`./` | _Infix_ | _Float-division_ | __number__ `"./"` _number_ -> _float_ | ||||
| | [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ `"%"` _integer_ -> _integer_ | ||||
| .5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ `"+"` _number_ -> _number_ | ||||
| | [blue]`+` | _Infix_ |  _String-concat_ | (_string_\|_number_) `"+"` (_string_\|_number_) -> _string_ | ||||
| | [blue]`+` | _Infix_ |  _List-join_ | _list_ `"+"` _list_ -> _list_ | ||||
| | [blue]`-` | _Infix_ | _Subtraction_ | _number_ `"-"` _number_ -> _number_ | ||||
| | [blue]`-` | _Infix_ | _List-difference_ | _list_ `"-"` _list_ -> _list_ | ||||
| .6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ `"<"` _comparable_ -> _boolean_ | ||||
| | [blue]`\<=` | _Infix_ |  _less-equal_ | _comparable_ `"\<="` _comparable_ -> _boolean_ | ||||
| | [blue]`>` | _Infix_ |  _greater_ | _comparable_ `">"` _comparable_ -> _boolean_ | ||||
| | [blue]`>=` | _Infix_ |  _greater-equal_ | _comparable_ `">="` _comparable_ -> _boolean_ | ||||
| | [blue]`==` | _Infix_ |  _equal_ | _comparable_ `"=="` _comparable_ -> _boolean_ | ||||
| | [blue]`!=` | _Infix_ |  _not-equal_ | _comparable_ `"!="` _comparable_ -> _boolean_ | ||||
| .1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | `"not"` _boolean_ -> _boolean_ | ||||
| .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_ | ||||
| 1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ "!" -> _integer_ | ||||
| 1+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| ("+"\|"-") _number_ -> _number_ | ||||
| .5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ "*" _number_ -> _number_ | ||||
| | [blue]`*` | _Infix_ | _String-repeat_ | _string_ "*" _integer_ -> _string_ | ||||
| | [blue]`/` | _Infix_ | _Division_ | _number_ "/" _number_ -> _number_ | ||||
| | [blue]`./` | _Infix_ | _Float-division_ | __number__ "./" _number_ -> _float_ | ||||
| | [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ "%" _integer_ -> _integer_ | ||||
| .5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ "+" _number_ -> _number_ | ||||
| | [blue]`+` | _Infix_ |  _String-concat_ | (_string_\|_number_) "+" (_string_\|_number_) -> _string_ | ||||
| | [blue]`+` | _Infix_ |  _List-join_ | _list_ "+" _list_ -> _list_ | ||||
| | [blue]`-` | _Infix_ | _Subtraction_ | _number_ "-" _number_ -> _number_ | ||||
| | [blue]`-` | _Infix_ | _List-difference_ | _list_ "-" _list_ -> _list_ | ||||
| .6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ "<" _comparable_ -> _boolean_ | ||||
| | [blue]`\<=` | _Infix_ |  _less-equal_ | _comparable_ "\<=" _comparable_ -> _boolean_ | ||||
| | [blue]`>` | _Infix_ |  _greater_ | _comparable_ ">" _comparable_ -> _boolean_ | ||||
| | [blue]`>=` | _Infix_ |  _greater-equal_ | _comparable_ ">=" _comparable_ -> _boolean_ | ||||
| | [blue]`==` | _Infix_ |  _equal_ | _comparable_ "==" _comparable_ -> _boolean_ | ||||
| | [blue]`!=` | _Infix_ |  _not-equal_ | _comparable_ "!=" _comparable_ -> _boolean_ | ||||
| .1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | "not" _boolean_ -> _boolean_ | ||||
| .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_ | ||||
| .1+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_ | ||||
| .1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_ | ||||
| |=== | ||||
|  | ||||
| @ -5,6 +5,7 @@ | ||||
| package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
| @ -52,7 +53,7 @@ func TestExpr(t *testing.T) { | ||||
| 		ImportOsFuncs(ctx) | ||||
| 		parser := NewParser(ctx) | ||||
| 
 | ||||
| 		logTest(t, i+1, "Expr", input.source, input.wantResult, input.wantErr) | ||||
| 		logTest(t, i+1, input.source, input.wantResult, input.wantErr) | ||||
| 
 | ||||
| 		r := strings.NewReader(input.source) | ||||
| 		scanner := NewScanner(r, DefaultTranslations()) | ||||
| @ -80,5 +81,5 @@ func TestExpr(t *testing.T) { | ||||
| 			failed++ | ||||
| 		} | ||||
| 	} | ||||
| 	t.Logf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed) | ||||
| 	t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed)) | ||||
| } | ||||
|  | ||||
| @ -1,33 +0,0 @@ | ||||
| builtin ["os.file", "base"]; | ||||
| 
 | ||||
| readInt=func(fh){ | ||||
| 	line=readFile(fh); | ||||
| 	line ? [nil] {nil} :: {int(line)} | ||||
| }; | ||||
| 
 | ||||
| ds={ | ||||
| 	"init":func(filename){ | ||||
| 		fh=openFile(filename); | ||||
| 		fh ? [nil] {nil} :: { @current=readInt(fh); @prev=@current }; | ||||
| 		fh | ||||
| 	}, | ||||
| 	"current":func(){ | ||||
| 		prev | ||||
| 	}, | ||||
| 	"next":func(fh){ | ||||
| 		current ?  | ||||
| 			[nil] {current} | ||||
| 			:: {@prev=current; @current=readInt(fh) but current} | ||||
| 	}, | ||||
| 	"clean":func(fh){ | ||||
| 		closeFile(fh) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| //;f=$(ds, "int.list") | ||||
| /* | ||||
| ;f++ | ||||
| ;f++ | ||||
| ;f++ | ||||
| */ | ||||
| //;add(f) | ||||
							
								
								
									
										57
									
								
								func-math.go
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								func-math.go
									
									
									
									
									
								
							| @ -9,40 +9,40 @@ import ( | ||||
| 	"io" | ||||
| ) | ||||
| 
 | ||||
| func checkNumberParamExpected(funcName string, paramValue any, paramPos, level, subPos int) (err error) { | ||||
| 	if !(isNumber(paramValue) || isFraction(paramValue)) /*|| isList(paramValue)*/ { | ||||
| 		err = fmt.Errorf("%s(): param nr %d (%d in %d) has wrong type %T, number expected", | ||||
| 			funcName, paramPos+1, subPos+1, level, paramValue) | ||||
| func checkNumberParamExpected(funcName string, paramValue any, paramPos int) (err error) { | ||||
| 	if !(isNumber(paramValue) || isList(paramValue) || isFraction(paramValue)) { | ||||
| 		err = fmt.Errorf("%s(): param nr %d has wrong type %T, number expected", funcName, paramPos+1, paramValue) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func doAdd(ctx ExprContext, name string, it Iterator, count, level int) (result any, err error) { | ||||
| func doAdd(ctx ExprContext, name string, it Iterator) (result any, err error) { | ||||
| 	var sumAsFloat, sumAsFract bool | ||||
| 	var floatSum float64 = 0.0 | ||||
| 	var intSum int64 = 0 | ||||
| 	var fractSum *fraction | ||||
| 	var v any | ||||
| 
 | ||||
| 	level++ | ||||
| 
 | ||||
| 	for v, err = it.Next(); err == nil; v, err = it.Next() { | ||||
| 		if list, ok := v.(*ListType); ok { | ||||
| 			v = NewListIterator(list, nil) | ||||
| 		} | ||||
| 		if subIter, ok := v.(Iterator); ok { | ||||
| 			if v, err = doAdd(ctx, name, subIter, count, level); err != nil { | ||||
| 			if v, err = doAdd(ctx, name, subIter); err != nil { | ||||
| 				break | ||||
| 			} | ||||
| 			if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(cleanName) { | ||||
| 				if _, err = extIter.CallOperation(cleanName, nil); err != nil { | ||||
| 			if subIter.HasOperation(cleanName) { | ||||
| 				if _, err = subIter.CallOperation(cleanName, nil); err != nil { | ||||
| 					return | ||||
| 				} | ||||
| 			} | ||||
| 		} else if err = checkNumberParamExpected(name, v, count, level, it.Index()); err != nil { | ||||
| 		} else { | ||||
| 			if err = checkNumberParamExpected(name, v, it.Index()); err != nil { | ||||
| 				break | ||||
| 			} | ||||
| 		count++ | ||||
| 			if list, ok := v.(*ListType); ok { | ||||
| 				if v, err = doAdd(ctx, name, NewListIterator(list)); err != nil { | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if !sumAsFloat { | ||||
| 			if isFloat(v) { | ||||
| @ -87,37 +87,38 @@ func doAdd(ctx ExprContext, name string, it Iterator, count, level int) (result | ||||
| } | ||||
| 
 | ||||
| func addFunc(ctx ExprContext, name string, args []any) (result any, err error) { | ||||
| 	result, err = doAdd(ctx, name, NewArrayIterator(args), 0, -1) | ||||
| 	result, err = doAdd(ctx, name, NewArrayIterator(args)) | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func doMul(ctx ExprContext, name string, it Iterator, count, level int) (result any, err error) { | ||||
| func doMul(ctx ExprContext, name string, it Iterator) (result any, err error) { | ||||
| 	var mulAsFloat, mulAsFract bool | ||||
| 	var floatProd float64 = 1.0 | ||||
| 	var intProd int64 = 1 | ||||
| 	var fractProd *fraction | ||||
| 	var v any | ||||
| 
 | ||||
| 	level++ | ||||
| 	for v, err = it.Next(); err == nil; v, err = it.Next() { | ||||
| 		if list, ok := v.(*ListType); ok { | ||||
| 			v = NewListIterator(list, nil) | ||||
| 		} | ||||
| 		if subIter, ok := v.(Iterator); ok { | ||||
| 			if v, err = doMul(ctx, name, subIter, count, level); err != nil { | ||||
| 			if v, err = doMul(ctx, name, subIter); err != nil { | ||||
| 				break | ||||
| 			} | ||||
| 			if extIter, ok := v.(ExtIterator); ok && extIter.HasOperation(cleanName) { | ||||
| 				if _, err = extIter.CallOperation(cleanName, nil); err != nil { | ||||
| 			if subIter.HasOperation(cleanName) { | ||||
| 				if _, err = subIter.CallOperation(cleanName, nil); err != nil { | ||||
| 					return | ||||
| 				} | ||||
| 			} | ||||
| 		} else { | ||||
| 			if err = checkNumberParamExpected(name, v, count, level, it.Index()); err != nil { | ||||
| 			if err = checkNumberParamExpected(name, v, it.Index()); err != nil { | ||||
| 				break | ||||
| 			} | ||||
| 
 | ||||
| 			if list, ok := v.(*ListType); ok { | ||||
| 				if v, err = doMul(ctx, name, NewListIterator(list)); err != nil { | ||||
| 					break | ||||
| 				} | ||||
| 			} | ||||
| 		count++ | ||||
| 		} | ||||
| 
 | ||||
| 		if !mulAsFloat { | ||||
| 			if isFloat(v) { | ||||
| @ -162,7 +163,7 @@ func doMul(ctx ExprContext, name string, it Iterator, count, level int) (result | ||||
| } | ||||
| 
 | ||||
| func mulFunc(ctx ExprContext, name string, args []any) (result any, err error) { | ||||
| 	result, err = doMul(ctx, name, NewArrayIterator(args), 0, -1) | ||||
| 	result, err = doMul(ctx, name, NewArrayIterator(args)) | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| @ -172,5 +173,5 @@ func ImportMathFuncs(ctx ExprContext) { | ||||
| } | ||||
| 
 | ||||
| func init() { | ||||
| 	registerImport("math.arith", ImportMathFuncs, "Functions add() and mul()") | ||||
| 	registerImport("math.arith", ImportMathFuncs, "Function add() and mul()") | ||||
| } | ||||
|  | ||||
| @ -40,7 +40,7 @@ func joinStrFunc(ctx ExprContext, name string, args []any) (result any, err erro | ||||
| 			result = "" | ||||
| 		} else if len(args) == 2 { | ||||
| 			if ls, ok := args[1].(*ListType); ok { | ||||
| 				result, err = doJoinStr(name, sep, NewListIterator(ls, nil)) | ||||
| 				result, err = doJoinStr(name, sep, NewListIterator(ls)) | ||||
| 			} else if it, ok := args[1].(Iterator); ok { | ||||
| 				result, err = doJoinStr(name, sep, it) | ||||
| 			} else { | ||||
|  | ||||
| @ -6,6 +6,7 @@ package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
| @ -49,7 +50,7 @@ func TestFuncs(t *testing.T) { | ||||
| 		// ImportOsFuncs(ctx)
 | ||||
| 		parser := NewParser(ctx) | ||||
| 
 | ||||
| 		logTest(t, i+1, "Funcs", input.source, input.wantResult, input.wantErr) | ||||
| 		logTest(t, i+1, input.source, input.wantResult, input.wantErr) | ||||
| 
 | ||||
| 		r := strings.NewReader(input.source) | ||||
| 		scanner := NewScanner(r, DefaultTranslations()) | ||||
| @ -77,5 +78,5 @@ func TestFuncs(t *testing.T) { | ||||
| 			failed++ | ||||
| 		} | ||||
| 	} | ||||
| 	t.Logf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed) | ||||
| 	t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed)) | ||||
| } | ||||
|  | ||||
							
								
								
									
										98
									
								
								iter-list.go
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								iter-list.go
									
									
									
									
									
								
							| @ -4,69 +4,26 @@ | ||||
| // iter-list.go
 | ||||
| package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| ) | ||||
| import "io" | ||||
| 
 | ||||
| type ListIterator struct { | ||||
| 	a     *ListType | ||||
| 	count int | ||||
| 	index int | ||||
| 	start int | ||||
| 	stop  int | ||||
| 	step  int | ||||
| } | ||||
| 
 | ||||
| func NewListIterator(list *ListType, args []any) (it *ListIterator) { | ||||
| 	var argc int = 0 | ||||
| 	listLen := len(([]any)(*list)) | ||||
| 	if args != nil { | ||||
| 		argc = len(args) | ||||
| 	} | ||||
| 	it = &ListIterator{a: list, count: 0, index: -1, start: 0, stop: listLen - 1, step: 1} | ||||
| 	if argc >= 1 { | ||||
| 		if i, err := toInt(args[0], "start index"); err == nil { | ||||
| 			if i < 0 { | ||||
| 				i = listLen + i | ||||
| 			} | ||||
| 			it.start = i | ||||
| 		} | ||||
| 		if argc >= 2 { | ||||
| 			if i, err := toInt(args[1], "stop index"); err == nil { | ||||
| 				if i < 0 { | ||||
| 					i = listLen + i | ||||
| 				} | ||||
| 				it.stop = i | ||||
| 			} | ||||
| 			if argc >= 3 { | ||||
| 				if i, err := toInt(args[2], "step"); err == nil { | ||||
| 					if i < 0 { | ||||
| 						i = -i | ||||
| 					} | ||||
| 					if it.start > it.stop { | ||||
| 						it.step = -i | ||||
| 					} else { | ||||
| 						it.step = i | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	it.index = it.start - it.step | ||||
| 	return | ||||
| func NewListIterator(list *ListType) *ListIterator { | ||||
| 	return &ListIterator{a: list, index: 0} | ||||
| } | ||||
| 
 | ||||
| func NewArrayIterator(array []any) (it *ListIterator) { | ||||
| 	it = &ListIterator{a: (*ListType)(&array), count: 0, index: -1, start: 0, stop: len(array) - 1, step: 1} | ||||
| 	return | ||||
| func NewArrayIterator(array []any) *ListIterator { | ||||
| 	return &ListIterator{a: (*ListType)(&array), index: 0} | ||||
| } | ||||
| 
 | ||||
| func NewAnyIterator(value any) (it *ListIterator) { | ||||
| 	if value == nil { | ||||
| 		it = NewArrayIterator([]any{}) | ||||
| 	} else if list, ok := value.(*ListType); ok { | ||||
| 		it = NewListIterator(list, nil) | ||||
| 		it = NewListIterator(list) | ||||
| 	} else if array, ok := value.([]any); ok { | ||||
| 		it = NewArrayIterator(array) | ||||
| 	} else if it1, ok := value.(*ListIterator); ok { | ||||
| @ -77,36 +34,17 @@ func NewAnyIterator(value any) (it *ListIterator) { | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) String() string { | ||||
| 	var l = 0 | ||||
| 	if it.a != nil { | ||||
| 		l = len(*it.a) | ||||
| 	} | ||||
| 	return fmt.Sprintf("$(#%d)", l) | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) HasOperation(name string) bool { | ||||
| 	yes := name == resetName || name == indexName || name == countName | ||||
| 	return yes | ||||
| 	return false | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) CallOperation(name string, args []any) (v any, err error) { | ||||
| 	switch name { | ||||
| 	case resetName: | ||||
| 		v, err = it.Reset() | ||||
| 	case indexName: | ||||
| 		v = int64(it.Index()) | ||||
| 	case countName: | ||||
| 		v = it.count | ||||
| 	default: | ||||
| 		err = errNoOperation(name) | ||||
| 	} | ||||
| 	return | ||||
| func (it *ListIterator) CallOperation(name string, args []any) (any, error) { | ||||
| 	return nil, errNoOperation(name) | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) Current() (item any, err error) { | ||||
| 	a := *(it.a) | ||||
| 	if it.index >= 0 && it.index <= it.stop { | ||||
| 	if it.index >= 0 && it.index < len(a) { | ||||
| 		item = a[it.index] | ||||
| 	} else { | ||||
| 		err = io.EOF | ||||
| @ -115,18 +53,18 @@ func (it *ListIterator) Current() (item any, err error) { | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) Next() (item any, err error) { | ||||
| 	it.index += it.step | ||||
| 	if item, err = it.Current(); err != io.EOF { | ||||
| 		it.count++ | ||||
| 		it.index++ | ||||
| 	} | ||||
| 	// if it.index < len(it.a) {
 | ||||
| 	// 	item = it.a[it.index]
 | ||||
| 	// 	it.index++
 | ||||
| 	// } else {
 | ||||
| 	// 	err = io.EOF
 | ||||
| 	// }
 | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) Index() int { | ||||
| 	return it.index | ||||
| } | ||||
| 
 | ||||
| func (it *ListIterator) Reset() (bool, error) { | ||||
| 	it.index = it.start | ||||
| 	return true, nil | ||||
| 	return it.index - 1 | ||||
| } | ||||
|  | ||||
| @ -1,18 +0,0 @@ | ||||
| ds={ | ||||
| 	"init":func(end){@end=end; @current=0; @prev=@current}, | ||||
| 	"current":func(){prev}, | ||||
| 	"next":func(){ | ||||
| 		(current <= end) ? [true] {@current=current+1; @prev=current} :: {nil} | ||||
| 	}, | ||||
| 	"reset":func(){@current=0; @prev=@current} | ||||
| } | ||||
| 
 | ||||
| // Example | ||||
| //; | ||||
| //it=$(ds,3); | ||||
| //it++; | ||||
| //it."reset" | ||||
| //it++; | ||||
| //it++; | ||||
| //add(it) | ||||
| 
 | ||||
							
								
								
									
										16
									
								
								iterator.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								iterator.go
									
									
									
									
									
								
							| @ -9,26 +9,10 @@ import ( | ||||
| 	"fmt" | ||||
| ) | ||||
| 
 | ||||
| // Operator names
 | ||||
| 
 | ||||
| const ( | ||||
| 	initName    = "init" | ||||
| 	cleanName   = "clean" | ||||
| 	resetName   = "reset" | ||||
| 	nextName    = "next" | ||||
| 	currentName = "current" | ||||
| 	indexName   = "index" | ||||
| 	countName   = "count" | ||||
| ) | ||||
| 
 | ||||
| type Iterator interface { | ||||
| 	Next() (item any, err error) // must return io.EOF after the last item
 | ||||
| 	Current() (item any, err error) | ||||
| 	Index() int | ||||
| } | ||||
| 
 | ||||
| type ExtIterator interface { | ||||
| 	Iterator | ||||
| 	HasOperation(name string) bool | ||||
| 	CallOperation(name string, args []any) (value any, err error) | ||||
| } | ||||
|  | ||||
| @ -6,7 +6,6 @@ package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| ) | ||||
| 
 | ||||
| // -------- function call term
 | ||||
| @ -22,20 +21,6 @@ func newFuncCallTerm(tk *Token, args []*term) *term { | ||||
| } | ||||
| 
 | ||||
| // -------- eval func call
 | ||||
| func checkFunctionCall(ctx ExprContext, name string, params []any) (err error) { | ||||
| 	if info, exists := ctx.GetFuncInfo(name); exists { | ||||
| 		if info.MinArgs() > len(params) { | ||||
| 			err = fmt.Errorf("too few params -- expected %d, got %d", info.MinArgs(), len(params)) | ||||
| 		} | ||||
| 		if info.MaxArgs() >= 0 && info.MaxArgs() < len(params) { | ||||
| 			err = fmt.Errorf("too much params -- expected %d, got %d", info.MaxArgs(), len(params)) | ||||
| 		} | ||||
| 	} else { | ||||
| 		err = fmt.Errorf("unknown function %s()", name) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) { | ||||
| 	ctx := cloneContext(parentCtx) | ||||
| 	name, _ := self.tk.Value.(string) | ||||
| @ -49,21 +34,19 @@ func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) { | ||||
| 		params[i] = param | ||||
| 	} | ||||
| 	if err == nil { | ||||
| 		if err = checkFunctionCall(ctx, name, params); err == nil { | ||||
| 		if v, err = ctx.Call(name, params); err == nil { | ||||
| 			exportObjects(parentCtx, ctx) | ||||
| 		} | ||||
| 	} | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| // -------- function definition term
 | ||||
| func newFuncDefTerm(tk *Token, args []*term) *term { | ||||
| 	return &term{ | ||||
| 		tk:       *tk, // value is the expression body
 | ||||
| 		tk:       *tk, | ||||
| 		parent:   nil, | ||||
| 		children: args, // function params
 | ||||
| 		children: args, // arg[0]=formal-param-list, arg[1]=*ast
 | ||||
| 		position: posLeaf, | ||||
| 		priority: priValue, | ||||
| 		evalFunc: evalFuncDef, | ||||
|  | ||||
| @ -12,7 +12,7 @@ import ( | ||||
| 
 | ||||
| // -------- iterator term
 | ||||
| 
 | ||||
| func newDsIteratorTerm(tk *Token, dsTerm *term, args []*term) *term { | ||||
| func newIteratorTerm(tk *Token, dsTerm *term, args []*term) *term { | ||||
| 	tk.Sym = SymIterator | ||||
| 
 | ||||
| 	children := make([]*term, 0, 1+len(args)) | ||||
| @ -28,18 +28,6 @@ func newDsIteratorTerm(tk *Token, dsTerm *term, args []*term) *term { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func newIteratorTerm(tk *Token, args []*term) *term { | ||||
| 	tk.Sym = SymIterator | ||||
| 	return &term{ | ||||
| 		tk:       *tk, | ||||
| 		parent:   nil, | ||||
| 		children: args, | ||||
| 		position: posLeaf, | ||||
| 		priority: priValue, | ||||
| 		evalFunc: evalIterator, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // -------- eval iterator
 | ||||
| 
 | ||||
| func evalTermArray(ctx ExprContext, a []*term) (values []any, err error) { | ||||
| @ -55,21 +43,59 @@ func evalTermArray(ctx ExprContext, a []*term) (values []any, err error) { | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func evalFirstChild(ctx ExprContext, self *term) (value any, err error) { | ||||
| // func getDataSourceDict(ctx ExprContext, self *term) (ds map[string]Functor, err error) {
 | ||||
| // 	var value any
 | ||||
| // 	if len(self.children) < 1 || self.children[0] == nil {
 | ||||
| // 		err = self.Errorf("missing the data-source parameter")
 | ||||
| // 		return
 | ||||
| // 	}
 | ||||
| 
 | ||||
| // 	if value, err = self.children[0].compute(ctx); err != nil {
 | ||||
| // 		return
 | ||||
| // 	}
 | ||||
| 
 | ||||
| // 	if dictAny, ok := value.(map[any]any); ok {
 | ||||
| // 		ds = make(map[string]Functor)
 | ||||
| // 		// required functions
 | ||||
| // 		for _, k := range []string{currentName, nextName} {
 | ||||
| // 			if item, exists := dictAny[k]; exists && item != nil {
 | ||||
| // 				if functor, ok := item.(*funcDefFunctor); ok {
 | ||||
| // 					ds[k] = functor
 | ||||
| // 				}
 | ||||
| // 			} else {
 | ||||
| // 				err = fmt.Errorf("the data-source must provide a non-nil %q operator", k)
 | ||||
| // 				return
 | ||||
| // 			}
 | ||||
| // 		}
 | ||||
| // 		// Optional functions
 | ||||
| // 		for _, k := range []string{initName, resetName, cleanName} {
 | ||||
| // 			if item, exists := dictAny[k]; exists && item != nil {
 | ||||
| // 				if functor, ok := item.(*funcDefFunctor); ok {
 | ||||
| // 					ds[k] = functor
 | ||||
| // 				}
 | ||||
| // 			}
 | ||||
| // 		}
 | ||||
| // 	} else {
 | ||||
| // 		err = self.Errorf("the first param (data-source) of an iterator must be a dict, not a %T", value)
 | ||||
| // 	}
 | ||||
| // 	return
 | ||||
| // }
 | ||||
| 
 | ||||
| func getDataSourceDict(ctx ExprContext, self *term) (ds map[string]Functor, err error) { | ||||
| 	var value any | ||||
| 	if len(self.children) < 1 || self.children[0] == nil { | ||||
| 		err = self.Errorf("missing the data-source parameter") | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	value, err = self.children[0].compute(ctx) | ||||
| 	if value, err = self.children[0].compute(ctx); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| func getDataSourceDict(ctx ExprContext, self *term, firstChildValue any) (ds map[string]Functor, err error) { | ||||
| 	if dictAny, ok := firstChildValue.(map[any]any); ok { | ||||
| 	requiredFields := []string{currentName, nextName} | ||||
| 	fieldsMask := 0b11 | ||||
| 	foundFields := 0 | ||||
| 	if dictAny, ok := value.(map[any]any); ok { | ||||
| 		ds = make(map[string]Functor) | ||||
| 		for keyAny, item := range dictAny { | ||||
| 			if key, ok := keyAny.(string); ok { | ||||
| @ -91,24 +117,22 @@ func getDataSourceDict(ctx ExprContext, self *term, firstChildValue any) (ds map | ||||
| 			} | ||||
| 			err = fmt.Errorf("the data-source must provide a non-nil %q operator(s)", strings.Join(missingFields, ", ")) | ||||
| 		} | ||||
| 	} else { | ||||
| 		err = self.Errorf("the first param (data-source) of an iterator must be a dict, not a %T", value) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func evalIterator(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	var firstChildValue any | ||||
| 	var ds map[string]Functor | ||||
| 
 | ||||
| 	if firstChildValue, err = evalFirstChild(ctx, self); err != nil { | ||||
| 	if ds, err = getDataSourceDict(ctx, self); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if ds, err = getDataSourceDict(ctx, self, firstChildValue); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if ds != nil { | ||||
| 	dc := newDataCursor(ctx, ds) | ||||
| 	// var it Iterator = dc
 | ||||
| 	// fmt.Println(it)
 | ||||
| 	if initFunc, exists := ds[initName]; exists && initFunc != nil { | ||||
| 		var args []any | ||||
| 		if len(self.children) > 1 { | ||||
| @ -132,53 +156,6 @@ func evalIterator(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	dc.resetFunc, _ = ds[resetName] | ||||
| 
 | ||||
| 	v = dc | ||||
| 	} else if list, ok := firstChildValue.(*ListType); ok { | ||||
| 		var args []any | ||||
| 		if args, err = evalSibling(ctx, self.children, nil); err == nil { | ||||
| 			v = NewListIterator(list, args) | ||||
| 		} | ||||
| 	} else { | ||||
| 		var list []any | ||||
| 		if list, err = evalSibling(ctx, self.children, firstChildValue); err == nil { | ||||
| 			v = NewArrayIterator(list) | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| // func evalChildren(ctx ExprContext, terms []*term, firstChildValue any) (list *ListType, err error) {
 | ||||
| // 	items := make(ListType, len(terms))
 | ||||
| // 	for i, tree := range terms {
 | ||||
| // 		var param any
 | ||||
| // 		if i == 0 && firstChildValue != nil {
 | ||||
| // 			param = firstChildValue
 | ||||
| // 		} else if param, err = tree.compute(ctx); err != nil {
 | ||||
| // 			break
 | ||||
| // 		}
 | ||||
| // 		items[i] = param
 | ||||
| // 	}
 | ||||
| // 	if err == nil {
 | ||||
| // 		list = &items
 | ||||
| // 	}
 | ||||
| // 	return
 | ||||
| // }
 | ||||
| 
 | ||||
| func evalSibling(ctx ExprContext, terms []*term, firstChildValue any) (list []any, err error) { | ||||
| 	items := make([]any, 0, len(terms)) | ||||
| 	for i, tree := range terms { | ||||
| 		var param any | ||||
| 		if i == 0 { | ||||
| 			if firstChildValue == nil { | ||||
| 				continue | ||||
| 			} | ||||
| 			param = firstChildValue | ||||
| 		} else if param, err = tree.compute(ctx); err != nil { | ||||
| 			break | ||||
| 		} | ||||
| 		items = append(items, param) | ||||
| 	} | ||||
| 	if err == nil { | ||||
| 		list = items | ||||
| 	} | ||||
| 
 | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -29,14 +29,7 @@ func evalAssign(ctx ExprContext, self *term) (v any, err error) { | ||||
| 
 | ||||
| 	if v, err = self.children[1].compute(ctx); err == nil { | ||||
| 		if functor, ok := v.(Functor); ok { | ||||
| 			var minArgs, maxArgs int = 0, 0 | ||||
| 
 | ||||
| 			if funcDef, ok := functor.(*funcDefFunctor); ok { | ||||
| 				l := len(funcDef.params) | ||||
| 				minArgs = l | ||||
| 				maxArgs = l | ||||
| 			} | ||||
| 			ctx.RegisterFunc(leftTerm.source(), functor, minArgs, maxArgs) | ||||
| 			ctx.RegisterFunc(leftTerm.source(), functor, 0, -1) | ||||
| 		} else { | ||||
| 			ctx.setVar(leftTerm.source(), v) | ||||
| 		} | ||||
|  | ||||
| @ -50,7 +50,7 @@ func evalBuiltin(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		} | ||||
| 	} | ||||
| 	if err == nil { | ||||
| 		v = int64(count) | ||||
| 		v = count | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -11,7 +11,7 @@ func newContextTerm(tk *Token) (inst *term) { | ||||
| 		tk:       *tk, | ||||
| 		children: make([]*term, 0, 1), | ||||
| 		position: posPrefix, | ||||
| 		priority: priIncDec, | ||||
| 		priority: priPrePost, | ||||
| 		evalFunc: evalContextValue, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -47,11 +47,10 @@ func evalDot(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	indexTerm := self.children[1] | ||||
| 
 | ||||
| 	switch unboxedValue := leftValue.(type) { | ||||
| 	case *ListType: | ||||
| 	case []any: | ||||
| 		var index int | ||||
| 		array := ([]any)(*unboxedValue) | ||||
| 		if index, err = verifyIndex(ctx, indexTerm, len(array)); err == nil { | ||||
| 			v = array[index] | ||||
| 		if index, err = verifyIndex(ctx, indexTerm, len(unboxedValue)); err == nil { | ||||
| 			v = unboxedValue[index] | ||||
| 		} | ||||
| 	case string: | ||||
| 		var index int | ||||
| @ -66,27 +65,17 @@ func evalDot(ctx ExprContext, self *term) (v any, err error) { | ||||
| 				err = fmt.Errorf("key %v does not belong to the dictionary", rightValue) | ||||
| 			} | ||||
| 		} | ||||
| 	// case *dataCursor:
 | ||||
| 	// 	if indexTerm.symbol() == SymIdentifier {
 | ||||
| 	// 		opName := indexTerm.source()
 | ||||
| 	// 		if opName == resetName {
 | ||||
| 	// 			_, err = unboxedValue.Reset()
 | ||||
| 	// 		} else if opName == cleanName {
 | ||||
| 	// 			_, err = unboxedValue.Clean()
 | ||||
| 	// 		} else {
 | ||||
| 	// 			err = indexTerm.Errorf("iterators do not support command %q", opName)
 | ||||
| 	// 		}
 | ||||
| 	// 		v = err == nil
 | ||||
| 	// 	}
 | ||||
| 	case ExtIterator: | ||||
| 	case *dataCursor: | ||||
| 		if indexTerm.symbol() == SymIdentifier { | ||||
| 			opName := indexTerm.source() | ||||
| 			if unboxedValue.HasOperation(opName) { | ||||
| 				v, err = unboxedValue.CallOperation(opName, []any{}) | ||||
| 			if opName == resetName { | ||||
| 				err = unboxedValue.Reset() | ||||
| 			} else if opName == cleanName { | ||||
| 				err = unboxedValue.Clean() | ||||
| 			} else { | ||||
| 				err = indexTerm.Errorf("this iterator do not support the %q command", opName) | ||||
| 				v = false | ||||
| 				err = indexTerm.Errorf("iterators do not support command %q", opName) | ||||
| 			} | ||||
| 			v = err == nil | ||||
| 		} | ||||
| 	default: | ||||
| 		err = self.errIncompatibleTypes(leftValue, rightValue) | ||||
|  | ||||
| @ -4,6 +4,7 @@ | ||||
| // operator-iter-value.go
 | ||||
| package expr | ||||
| 
 | ||||
| 
 | ||||
| //-------- iter value term
 | ||||
| 
 | ||||
| func newIterValueTerm(tk *Token) (inst *term) { | ||||
| @ -17,16 +18,16 @@ func newIterValueTerm(tk *Token) (inst *term) { | ||||
| } | ||||
| 
 | ||||
| func evalIterValue(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	var childValue any | ||||
| 	var leftValue any | ||||
| 
 | ||||
| 	if childValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 	if leftValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if it, ok := childValue.(Iterator); ok { | ||||
| 		v, err = it.Current() | ||||
| 	if dc, ok := leftValue.(*dataCursor); ok { | ||||
| 		v, err = dc.Current() | ||||
| 	} else { | ||||
| 		err = self.errIncompatibleType(childValue) | ||||
| 		err = self.errIncompatibleType(leftValue) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -17,27 +17,22 @@ func newLengthTerm(tk *Token) (inst *term) { | ||||
| } | ||||
| 
 | ||||
| func evalLength(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	var childValue any | ||||
| 	var rightValue any | ||||
| 
 | ||||
| 	if childValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 	if rightValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if isList(childValue) { | ||||
| 		list, _ := childValue.([]any) | ||||
| 	if isList(rightValue) { | ||||
| 		list, _ := rightValue.([]any) | ||||
| 		v = int64(len(list)) | ||||
| 	} else if isString(childValue) { | ||||
| 		s, _ := childValue.(string) | ||||
| 	} else if isString(rightValue) { | ||||
| 		s, _ := rightValue.(string) | ||||
| 		v = int64(len(s)) | ||||
| 	} else if it, ok := childValue.(Iterator); ok { | ||||
| 		if extIt, ok := childValue.(ExtIterator); ok && extIt.HasOperation(countName) { | ||||
| 			count, _ := extIt.CallOperation(countName, nil) | ||||
| 			v, _ = toInt(count, "") | ||||
| 	} else if it, ok := rightValue.(Iterator); ok { | ||||
| 		v = int64(it.Index()) | ||||
| 	} else { | ||||
| 			v = int64(it.Index() + 1) | ||||
| 		} | ||||
| 	} else { | ||||
| 		err = self.errIncompatibleType(childValue) | ||||
| 		err = self.errIncompatibleType(rightValue) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @ -12,25 +12,25 @@ func newPostIncTerm(tk *Token) *term { | ||||
| 		parent:   nil, | ||||
| 		children: make([]*term, 0, 1), | ||||
| 		position: posPostfix, | ||||
| 		priority: priIncDec, | ||||
| 		priority: priPrePost, | ||||
| 		evalFunc: evalPostInc, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func evalPostInc(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	var childValue any | ||||
| 	if childValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 	var leftValue any | ||||
| 	if leftValue, err = self.evalPrefix(ctx); err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if it, ok := childValue.(Iterator); ok { | ||||
| 		v, err = it.Next() | ||||
| 	} else if isInteger(childValue) && self.children[0].symbol() == SymIdentifier { | ||||
| 		v = childValue | ||||
| 		i, _ := childValue.(int64) | ||||
| 	if dc, ok := leftValue.(*dataCursor); ok { | ||||
| 		v, err = dc.Next() | ||||
| 	} else if isInteger(leftValue) && self.children[0].symbol() == SymIdentifier { | ||||
| 		v = leftValue | ||||
| 		i, _ := leftValue.(int64) | ||||
| 		ctx.SetVar(self.children[0].source(), i+1) | ||||
| 	} else { | ||||
| 		err = self.errIncompatibleType(childValue) | ||||
| 		self.errIncompatibleType(leftValue) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|  | ||||
							
								
								
									
										97
									
								
								parser.go
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								parser.go
									
									
									
									
									
								
							| @ -62,45 +62,30 @@ func (self *parser) parseFuncCall(scanner *scanner, allowVarRef bool, tk *Token) | ||||
| // 	// Example: "add = func(x,y) {x+y}
 | ||||
| // 	var body *ast
 | ||||
| // 	args := make([]*term, 0)
 | ||||
| // 	lastSym := SymUnknown
 | ||||
| // 	itemExpected := false
 | ||||
| // 	tk := scanner.Previous()
 | ||||
| // 	for lastSym != SymClosedRound && lastSym != SymEos {
 | ||||
| // 		var subTree *ast
 | ||||
| // 		if subTree, err = self.parseItem(scanner, true, SymComma, SymClosedRound); err == nil {
 | ||||
| // 			if subTree.root != nil {
 | ||||
| // 				if subTree.root.symbol() == SymIdentifier {
 | ||||
| // 					args = append(args, subTree.root)
 | ||||
| // 	tk := scanner.Next()
 | ||||
| // 	for tk.Sym != SymClosedRound && tk.Sym != SymEos {
 | ||||
| // 		if tk.Sym == SymIdentifier {
 | ||||
| // 			t := newTerm(tk, nil)
 | ||||
| // 			args = append(args, t)
 | ||||
| // 		} else {
 | ||||
| // 					err = tk.ErrorExpectedGotString("param-name", subTree.root.String())
 | ||||
| // 				}
 | ||||
| // 			} else if itemExpected {
 | ||||
| // 				prev := scanner.Previous()
 | ||||
| // 				err = prev.ErrorExpectedGot("function-param")
 | ||||
| // 			err = tk.Errorf("invalid param %q, variable identifier expected", tk.source)
 | ||||
| // 			break
 | ||||
| // 		}
 | ||||
| // 		} else {
 | ||||
| // 			break
 | ||||
| // 		tk = scanner.Next()
 | ||||
| // 	}
 | ||||
| // 		lastSym = scanner.Previous().Sym
 | ||||
| // 		itemExpected = lastSym == SymComma
 | ||||
| // 	}
 | ||||
| 
 | ||||
| // 	if err == nil && lastSym != SymClosedRound {
 | ||||
| // 		err = tk.ErrorExpectedGot(")")
 | ||||
| // 	if err == nil && tk.Sym != SymClosedRound {
 | ||||
| // 		err = tk.Errorf("unterminate function params list")
 | ||||
| // 	}
 | ||||
| // 	if err == nil {
 | ||||
| // 		tk = scanner.Next()
 | ||||
| // 		if tk.Sym == SymOpenBrace {
 | ||||
| // 			body, err = self.parseGeneral(scanner, true, true, SymClosedBrace)
 | ||||
| // 		} else {
 | ||||
| // 			err = tk.ErrorExpectedGot("{")
 | ||||
| // 		}
 | ||||
| // 	}
 | ||||
| // 	if err == nil {
 | ||||
| // 		// TODO Check arguments
 | ||||
| // 		if scanner.Previous().Sym != SymClosedBrace {
 | ||||
| // 			err = scanner.Previous().ErrorExpectedGot("}")
 | ||||
| // 			err = scanner.Previous().Errorf("not properly terminated function body")
 | ||||
| // 		} else {
 | ||||
| // 			tk = scanner.makeValueToken(SymExpression, "", body)
 | ||||
| // 			tree = newFuncDefTerm(tk, args)
 | ||||
| @ -117,14 +102,20 @@ func (self *parser) parseFuncDef(scanner *scanner) (tree *term, err error) { | ||||
| 	itemExpected := false | ||||
| 	tk := scanner.Previous() | ||||
| 	for lastSym != SymClosedRound && lastSym != SymEos { | ||||
| 		tk = scanner.Next() | ||||
| 		if tk.IsSymbol(SymIdentifier) { | ||||
| 			param := newTerm(tk, nil) | ||||
| 			args = append(args, param) | ||||
| 			tk = scanner.Next() | ||||
| 		var subTree *ast | ||||
| 		if subTree, err = self.parseItem(scanner, true, SymComma, SymClosedRound); err == nil { | ||||
| 			if subTree.root != nil { | ||||
| 				if subTree.root.symbol() == SymIdentifier { | ||||
| 					args = append(args, subTree.root) | ||||
| 				} else { | ||||
| 					err = tk.Errorf("exptected identifier, got %q", subTree.root) | ||||
| 				} | ||||
| 			} else if itemExpected { | ||||
| 				prev := scanner.Previous() | ||||
| 			err = prev.ErrorExpectedGot("function-param") | ||||
| 				err = prev.Errorf("expected function parameter, got %q", prev) | ||||
| 				break | ||||
| 			} | ||||
| 		} else { | ||||
| 			break | ||||
| 		} | ||||
| 		lastSym = scanner.Previous().Sym | ||||
| @ -132,20 +123,18 @@ func (self *parser) parseFuncDef(scanner *scanner) (tree *term, err error) { | ||||
| 	} | ||||
| 
 | ||||
| 	if err == nil && lastSym != SymClosedRound { | ||||
| 		err = tk.ErrorExpectedGot(")") | ||||
| 		err = tk.Errorf("unterminated function parameters list") | ||||
| 	} | ||||
| 	if err == nil { | ||||
| 		tk = scanner.Next() | ||||
| 		if tk.IsSymbol(SymOpenBrace) { | ||||
| 		if tk.Sym == SymOpenBrace { | ||||
| 			body, err = self.parseGeneral(scanner, true, true, SymClosedBrace) | ||||
| 		} else { | ||||
| 			err = tk.ErrorExpectedGot("{") | ||||
| 		} | ||||
| 	} | ||||
| 	if err == nil { | ||||
| 		// TODO Check arguments
 | ||||
| 		if scanner.Previous().Sym != SymClosedBrace { | ||||
| 			err = scanner.Previous().ErrorExpectedGot("}") | ||||
| 			err = scanner.Previous().Errorf("not properly terminated function body") | ||||
| 		} else { | ||||
| 			tk = scanner.makeValueToken(SymExpression, "", body) | ||||
| 			tree = newFuncDefTerm(tk, args) | ||||
| @ -165,7 +154,7 @@ func (self *parser) parseList(scanner *scanner, allowVarRef bool) (subtree *term | ||||
| 				args = append(args, subTree.root) | ||||
| 			} else if itemExpected { | ||||
| 				prev := scanner.Previous() | ||||
| 				err = prev.ErrorExpectedGot("list-item") | ||||
| 				err = prev.Errorf("expected list item, got %q", prev) | ||||
| 				break | ||||
| 			} | ||||
| 		} else { | ||||
| @ -177,7 +166,7 @@ func (self *parser) parseList(scanner *scanner, allowVarRef bool) (subtree *term | ||||
| 	if err == nil { | ||||
| 		// TODO Check arguments
 | ||||
| 		if lastSym != SymClosedSquare { | ||||
| 			err = scanner.Previous().ErrorExpectedGot("]") | ||||
| 			err = scanner.Previous().Errorf("unterminate items list") | ||||
| 		} else { | ||||
| 			subtree = newListTerm(args) | ||||
| 		} | ||||
| @ -186,18 +175,29 @@ func (self *parser) parseList(scanner *scanner, allowVarRef bool) (subtree *term | ||||
| } | ||||
| 
 | ||||
| func (self *parser) parseIterDef(scanner *scanner, allowVarRef bool) (subtree *term, err error) { | ||||
| 	var ds *term | ||||
| 	tk := scanner.Previous() | ||||
| 	args := make([]*term, 0) | ||||
| 	lastSym := SymUnknown | ||||
| 	dsExpected := true | ||||
| 	itemExpected := false | ||||
| 	for lastSym != SymClosedRound && lastSym != SymEos { | ||||
| 		var subTree *ast | ||||
| 		if subTree, err = self.parseItem(scanner, allowVarRef, SymComma, SymClosedRound); err == nil { | ||||
| 			if subTree.root != nil { | ||||
| 				if dsExpected { | ||||
| 					if sym := subTree.root.symbol(); sym == SymDict || sym == SymIdentifier { | ||||
| 						ds = subTree.root | ||||
| 					} else { | ||||
| 						err = subTree.root.Errorf("data-source dictionary expected, got %q", subTree.root.source()) | ||||
| 					} | ||||
| 					dsExpected = false | ||||
| 				} else { | ||||
| 					args = append(args, subTree.root) | ||||
| 				} | ||||
| 			} else if itemExpected { | ||||
| 				prev := scanner.Previous() | ||||
| 				err = prev.ErrorExpectedGot("iterator-param") | ||||
| 				err = prev.Errorf("expected iterator argument, got %q", prev) | ||||
| 				break | ||||
| 			} | ||||
| 		} else { | ||||
| @ -209,9 +209,11 @@ func (self *parser) parseIterDef(scanner *scanner, allowVarRef bool) (subtree *t | ||||
| 	if err == nil { | ||||
| 		// TODO Check arguments
 | ||||
| 		if lastSym != SymClosedRound { | ||||
| 			err = scanner.Previous().ErrorExpectedGot(")") | ||||
| 			err = scanner.Previous().Errorf("unterminate iterator param list") | ||||
| 		} else if ds != nil { | ||||
| 			subtree = newIteratorTerm(tk, ds, args) | ||||
| 		} else { | ||||
| 			subtree = newIteratorTerm(tk, args) | ||||
| 			tk.Errorf("missing data-source param") | ||||
| 		} | ||||
| 	} | ||||
| 	return | ||||
| @ -229,13 +231,12 @@ func (self *parser) parseDictKey(scanner *scanner, allowVarRef bool) (key any, e | ||||
| 	if tk.Sym == SymInteger || tk.Sym == SymString { | ||||
| 		tkSep := scanner.Next() | ||||
| 		if tkSep.Sym != SymColon { | ||||
| 			err = tkSep.ErrorExpectedGot(":") | ||||
| 			err = tkSep.Errorf("expected \":\", got %q", tkSep) | ||||
| 		} else { | ||||
| 			key = tk.Value | ||||
| 		} | ||||
| 	} else { | ||||
| 		// err = tk.Errorf("expected dictionary key or closed brace, got %q", tk)
 | ||||
| 		err = tk.ErrorExpectedGot("dictionary-key or }") | ||||
| 		err = tk.Errorf("expected dictionary key or closed brace, got %q", tk) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
| @ -253,7 +254,7 @@ func (self *parser) parseDictionary(scanner *scanner, allowVarRef bool) (subtree | ||||
| 			tk := scanner.Previous() | ||||
| 			lastSym = tk.Sym | ||||
| 			if itemExpected { | ||||
| 				err = tk.ErrorExpectedGot("dictionary-key") | ||||
| 				err = tk.Errorf("expected dictionary key, got %q", tk) | ||||
| 			} | ||||
| 			break | ||||
| 		} | ||||
| @ -262,7 +263,7 @@ func (self *parser) parseDictionary(scanner *scanner, allowVarRef bool) (subtree | ||||
| 				args[key] = subTree.root | ||||
| 			} else if key != nil { | ||||
| 				prev := scanner.Previous() | ||||
| 				err = prev.ErrorExpectedGot("dictionary-value") | ||||
| 				err = prev.Errorf("expected dictionary value, got %q", prev) | ||||
| 				break | ||||
| 			} | ||||
| 		} else { | ||||
| @ -274,7 +275,7 @@ func (self *parser) parseDictionary(scanner *scanner, allowVarRef bool) (subtree | ||||
| 	if err == nil { | ||||
| 		// TODO Check arguments
 | ||||
| 		if lastSym != SymClosedBrace { | ||||
| 			err = scanner.Previous().ErrorExpectedGot("}") | ||||
| 			err = scanner.Previous().Errorf("unterminated dictionary") | ||||
| 		} else { | ||||
| 			subtree = newDictTerm(args) | ||||
| 		} | ||||
| @ -308,7 +309,7 @@ func (self *parser) parseSelectorCase(scanner *scanner, allowVarRef bool, defaul | ||||
| 			return | ||||
| 		} | ||||
| 	} else { | ||||
| 		err = tk.ErrorExpectedGot("{") | ||||
| 		err = tk.Errorf("selector-case expected, got %q", tk.source) | ||||
| 	} | ||||
| 
 | ||||
| 	if err == nil { | ||||
|  | ||||
| @ -6,6 +6,7 @@ package expr | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| @ -159,7 +160,7 @@ func TestParser(t *testing.T) { | ||||
| 		/* 138 */ {`nil`, nil, nil}, | ||||
| 		/* 139 */ {`null`, nil, errors.New(`undefined variable or function "null"`)}, | ||||
| 		/* 140 */ {`{"key"}`, nil, errors.New(`[1:8] expected ":", got "}"`)}, | ||||
| 		/* 141 */ {`{"key":}`, nil, errors.New(`[1:9] expected "dictionary-value", got "}"`)}, | ||||
| 		/* 141 */ {`{"key":}`, nil, errors.New(`[1:9] expected dictionary value, got "}"`)}, | ||||
| 		/* 142 */ {`{}`, map[any]any{}, nil}, | ||||
| 		/* 143 */ {`1|2`, newFraction(1, 2), nil}, | ||||
| 		/* 144 */ {`1|2 + 1`, newFraction(3, 2), nil}, | ||||
| @ -173,15 +174,11 @@ func TestParser(t *testing.T) { | ||||
| 		/* 152 */ {`builtin "math.arith"; mul(1|2, 1.0, 2)`, float64(1.0), nil}, | ||||
| 		/* 153 */ {`include "iterator.expr"; it=$(ds,3); ()it`, int64(0), nil}, | ||||
| 		/* 154 */ {`include "iterator.expr"; it=$(ds,3); it++; it++`, int64(1), nil}, | ||||
| 		/* 155 */ {`include "iterator.expr"; it=$(ds,3); it++; it++; #it`, int64(2), nil}, | ||||
| 		/* 155 */ {`include "iterator.expr"; it=$(ds,3); it++; it++; #it`, int64(1), nil}, | ||||
| 		/* 156 */ {`include "iterator.expr"; it=$(ds,3); it++; it++; it.reset; ()it`, int64(0), nil}, | ||||
| 		/* 157 */ {`builtin "math.arith"; include "iterator.expr"; it=$(ds,3); add(it)`, int64(6), nil}, | ||||
| 		/* 158 */ {`builtin "math.arith"; include "iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil}, | ||||
| 		/* 159 */ {`include "file-reader.expr"; it=$(ds,"int.list"); mul(it)`, int64(12000), nil}, | ||||
| 		/* 160 */ {`include "file-reader.expr"; it=$(ds,"int.list"); it++; it.index`, int64(0), nil}, | ||||
| 		/* 161 */ {`include "file-reader.expr"; it=$(ds,"int.list"); it.clean`, true, nil}, | ||||
| 		/* 162 */ {`builtin "os.file"`, int64(1), nil}, | ||||
| 		/* 163 */ {`it=$(1,2,3); it++`, int64(1), nil}, | ||||
| 	} | ||||
| 	check_env_expr_path := 113 | ||||
| 
 | ||||
| @ -189,7 +186,7 @@ func TestParser(t *testing.T) { | ||||
| 	failed := 0 | ||||
| 
 | ||||
| 	// inputs1 := []inputType{
 | ||||
| 	// 	/* 159 */ {`include "file-reader.expr"; it=$(ds,"int.list"); mul(it)`, int64(12000), nil},
 | ||||
| 	// 	/* 140 */ {`ds={}; $(ds)`, nil, nil},
 | ||||
| 	// }
 | ||||
| 
 | ||||
| 	for i, input := range inputs { | ||||
| @ -204,7 +201,7 @@ func TestParser(t *testing.T) { | ||||
| 		ImportImportFuncs(ctx) | ||||
| 		parser := NewParser(ctx) | ||||
| 
 | ||||
| 		logTest(t, i+1, "General", input.source, input.wantResult, input.wantErr) | ||||
| 		logTest(t, i+1, input.source, input.wantResult, input.wantErr) | ||||
| 
 | ||||
| 		r := strings.NewReader(input.source) | ||||
| 		scanner := NewScanner(r, DefaultTranslations()) | ||||
| @ -237,7 +234,7 @@ func TestParser(t *testing.T) { | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	t.Logf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed) | ||||
| 	t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed)) | ||||
| } | ||||
| 
 | ||||
| func TestListParser(t *testing.T) { | ||||
| @ -257,9 +254,8 @@ func TestListParser(t *testing.T) { | ||||
| 		/*  7 */ {`add([1,4,3,2])`, int64(10), nil}, | ||||
| 		/*  8 */ {`add([1,[2,2],3,2])`, int64(10), nil}, | ||||
| 		/*  9 */ {`mul([1,4,3.0,2])`, float64(24.0), nil}, | ||||
| 		/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 (2 in 1) has wrong type string, number expected`)}, | ||||
| 		/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 has wrong type string, number expected`)}, | ||||
| 		/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil}, | ||||
| 
 | ||||
| 		// /*  8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil},
 | ||||
| 		// /*  9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil},
 | ||||
| 	} | ||||
| @ -282,7 +278,7 @@ func TestListParser(t *testing.T) { | ||||
| 		ImportMathFuncs(ctx) | ||||
| 		parser := NewParser(ctx) | ||||
| 
 | ||||
| 		logTest(t, i+1, "List", input.source, input.wantResult, input.wantErr) | ||||
| 		logTest(t, i+1, input.source, input.wantResult, input.wantErr) | ||||
| 
 | ||||
| 		r := strings.NewReader(input.source) | ||||
| 		scanner := NewScanner(r, DefaultTranslations()) | ||||
| @ -334,13 +330,13 @@ func TestListParser(t *testing.T) { | ||||
| 			failed++ | ||||
| 		} | ||||
| 	} | ||||
| 	t.Logf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed) | ||||
| 	t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed)) | ||||
| } | ||||
| 
 | ||||
| func logTest(t *testing.T, n int, section, source string, wantResult any, wantErr error) { | ||||
| func logTest(t *testing.T, n int, source string, wantResult any, wantErr error) { | ||||
| 	if wantErr == nil { | ||||
| 		t.Logf("[+]%s nr %3d -- %q  --> %v", section, n, source, wantResult) | ||||
| 		t.Log(fmt.Sprintf("[+]Test nr %3d -- %q  --> %v", n, source, wantResult)) | ||||
| 	} else { | ||||
| 		t.Logf("[-]%s nr %3d -- %q  --> %v", section, n, source, wantErr) | ||||
| 		t.Log(fmt.Sprintf("[-]Test nr %3d -- %q  --> %v", n, source, wantErr)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -160,12 +160,6 @@ func (self *scanner) fetchNextToken() (tk *Token) { | ||||
| 			//} else if next == '/' {
 | ||||
| 			if next, _ := self.peek(); next == '/' { | ||||
| 				tk = self.moveOn(SymDotSlash, ch, next) | ||||
| 			} else if next == '.' { | ||||
| 				if next1, _ := self.peek(); next1 == '.' { | ||||
| 					tk = self.moveOn(SymTripleDot, ch, next, next1) | ||||
| 				} else { | ||||
| 					tk = self.moveOn(SymDoubleDot, ch, next) | ||||
| 				} | ||||
| 			} else { | ||||
| 				tk = self.makeToken(SymDot, ch) | ||||
| 			} | ||||
|  | ||||
| @ -65,8 +65,6 @@ const ( | ||||
| 	SymDollarRound                       //  54: '$('
 | ||||
| 	SymOpenClosedRound                   //  55: '()'
 | ||||
| 	SymDoubleDollar                      //  56: '$$'
 | ||||
| 	SymDoubleDot                         //  57: '..'
 | ||||
| 	SymTripleDot                         //  58: '...'
 | ||||
| 	SymChangeSign | ||||
| 	SymUnchangeSign | ||||
| 	SymIdentifier | ||||
|  | ||||
							
								
								
									
										2
									
								
								term.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								term.go
									
									
									
									
									
								
							| @ -26,7 +26,7 @@ const ( | ||||
| 	priFact | ||||
| 	priIterValue | ||||
| 	priCoalesce | ||||
| 	priIncDec | ||||
| 	priPrePost | ||||
| 	priDot | ||||
| 	priValue | ||||
| ) | ||||
|  | ||||
							
								
								
									
										16
									
								
								token.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								token.go
									
									
									
									
									
								
							| @ -46,7 +46,7 @@ func NewValueToken(row, col int, sym Symbol, source string, value any) *Token { | ||||
| 
 | ||||
| func NewErrorToken(row, col int, err error) *Token { | ||||
| 	if err == io.EOF { | ||||
| 		return NewToken(row, col, SymEos, "<EOF>") | ||||
| 		return NewToken(row, col, SymEos, "") | ||||
| 	} | ||||
| 	return NewValueToken(row, col, SymError, fmt.Sprintf("[%d:%d]", row, col), err) | ||||
| } | ||||
| @ -63,10 +63,6 @@ func (tk *Token) IsTerm(termSymbols []Symbol) bool { | ||||
| 	return tk.IsEos() || tk.IsError() || (termSymbols != nil && slices.Index(termSymbols, tk.Sym) >= 0) | ||||
| } | ||||
| 
 | ||||
| func (tk *Token) IsSymbol(sym Symbol) bool { | ||||
| 	return tk.Sym == sym | ||||
| } | ||||
| 
 | ||||
| func (self *Token) Errorf(template string, args ...any) (err error) { | ||||
| 	err = fmt.Errorf(fmt.Sprintf("[%d:%d] ", self.row, self.col)+template, args...) | ||||
| 	return | ||||
| @ -85,13 +81,3 @@ func (self *Token) Errors(msg string) (err error) { | ||||
| 	err = fmt.Errorf("[%d:%d] %v", self.row, self.col, msg) | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (self *Token) ErrorExpectedGot(symbol string) (err error) { | ||||
| 	err = fmt.Errorf("[%d:%d] expected %q, got %q", self.row, self.col, symbol, self) | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| func (self *Token) ErrorExpectedGotString(symbol, got string) (err error) { | ||||
| 	err = fmt.Errorf("[%d:%d] expected %q, got %q", self.row, self.col, symbol, got) | ||||
| 	return | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user