Compare commits
	
		
			No commits in common. "8ee0bb5701de4db9c5da4fbee7c0fc7947276f38" and "34b779917799c4450d17bd7803952c9522dc7b40" have entirely different histories.
		
	
	
		
			8ee0bb5701
			...
			34b7799177
		
	
		
| @ -40,19 +40,17 @@ Here are some examples of execution. | ||||
| ---- | ||||
| # Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program. | ||||
| 
 | ||||
| [user]$ tools/expr -- Expressions calculator v1.7.0,2024/05/08 (celestino.amoroso@portale-stac.it) | ||||
| [user]$ tools/expr -- Expressions calculator v1.6.1,2024/05/06 (celestino.amoroso@portale-stac.it) | ||||
| 	Type help to get the list of command. | ||||
| 	See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc | ||||
| 
 | ||||
| >>> help | ||||
| --- REPL commands: | ||||
|         base -- Set the integer output base: 2, 8, 10, or 16 | ||||
|         exit -- Exit the program | ||||
|         help -- Show command list | ||||
|           ml -- Enable/Disable multi-line output | ||||
|         mods -- List builtin modules | ||||
|       source -- Load a file as input | ||||
|          tty -- Enable/Disable ansi output <1> | ||||
|         exit -- Exit the program | ||||
| 
 | ||||
| --- Command line options: | ||||
|     -b <builtin>       Import builtin modules. | ||||
| @ -64,8 +62,6 @@ Here are some examples of execution. | ||||
|     -m, --modules      List all builtin modules | ||||
|     -p                 Print prefix form | ||||
|     -t                 Print tree form <2> | ||||
| 
 | ||||
| >>>  | ||||
| ---- | ||||
| 
 | ||||
| <1> Only available for single fraction values | ||||
|  | ||||
| @ -604,19 +604,17 @@ Here are some examples of execution.</p> | ||||
| <div class="content"> | ||||
| <pre class="rouge highlight"><code data-lang="shell"><span class="c"># Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program.</span> | ||||
| 
 | ||||
| <span class="o">[</span>user]<span class="nv">$ </span>tools/expr <span class="nt">--</span> Expressions calculator v1.7.0,2024/05/08 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span> | ||||
| <span class="o">[</span>user]<span class="nv">$ </span>tools/expr <span class="nt">--</span> Expressions calculator v1.6.1,2024/05/06 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span> | ||||
| 	Type <span class="nb">help </span>to get the list of command. | ||||
| 	See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc | ||||
| 
 | ||||
| <span class="o">>>></span> <span class="nb">help</span> | ||||
| <span class="nt">---</span> REPL commands: | ||||
|         base <span class="nt">--</span> Set the integer output base: 2, 8, 10, or 16 | ||||
|         <span class="nb">exit</span> <span class="nt">--</span> Exit the program | ||||
|         <span class="nb">help</span> <span class="nt">--</span> Show <span class="nb">command </span>list | ||||
|           ml <span class="nt">--</span> Enable/Disable multi-line output | ||||
|         mods <span class="nt">--</span> List <span class="nb">builtin </span>modules | ||||
|       <span class="nb">source</span> <span class="nt">--</span> Load a file as input | ||||
|          <span class="nb">tty</span> <span class="nt">--</span> Enable/Disable ansi output <i class="conum" data-value="1"></i><b>(1)</b> | ||||
|         <span class="nb">exit</span> <span class="nt">--</span> Exit the program | ||||
| 
 | ||||
| <span class="nt">---</span> Command line options: | ||||
|     <span class="nt">-b</span> <<span class="nb">builtin</span><span class="o">></span>       Import <span class="nb">builtin </span>modules. | ||||
| @ -627,9 +625,7 @@ Here are some examples of execution.</p> | ||||
|     <span class="nt">-h</span>, <span class="nt">--help</span>, <span class="nb">help   </span>Show this <span class="nb">help </span>menu | ||||
|     <span class="nt">-m</span>, <span class="nt">--modules</span>      List all <span class="nb">builtin </span>modules | ||||
|     <span class="nt">-p</span>                 Print prefix form | ||||
|     <span class="nt">-t</span>                 Print tree form <i class="conum" data-value="2"></i><b>(2)</b> | ||||
| 
 | ||||
| <span class="o">>>></span></code></pre> | ||||
|     <span class="nt">-t</span>                 Print tree form <i class="conum" data-value="2"></i><b>(2)</b></code></pre> | ||||
| </div> | ||||
| </div> | ||||
| <div class="colist arabic"> | ||||
| @ -1393,7 +1389,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier. | ||||
| </div> | ||||
| <div id="footer"> | ||||
| <div id="footer-text"> | ||||
| Last updated 2024-05-08 07:50:05 +0200 | ||||
| Last updated 2024-05-07 07:20:42 +0200 | ||||
| </div> | ||||
| </div> | ||||
| </body> | ||||
|  | ||||
| @ -9,10 +9,6 @@ type FmtOpt uint16 | ||||
| const ( | ||||
| 	TTY FmtOpt = 1 << iota | ||||
| 	MultiLine | ||||
| 	Base2 | ||||
| 	Base8 | ||||
| 	Base10 | ||||
| 	Base16 | ||||
| ) | ||||
| 
 | ||||
| type Formatter interface { | ||||
|  | ||||
| @ -35,7 +35,7 @@ func importGeneral(ctx ExprContext, name string, args []any) (result any, err er | ||||
| } | ||||
| 
 | ||||
| func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) { | ||||
| 	if !(IsString(paramValue) /*|| isList(paramValue)*/) { | ||||
| 	if !(isString(paramValue) /*|| isList(paramValue)*/) { | ||||
| 		err = fmt.Errorf("%s(): param nr %d has wrong type %T, string expected", funcName, paramPos+1, paramValue) | ||||
| 	} | ||||
| 	return | ||||
|  | ||||
| @ -10,7 +10,7 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| func checkNumberParamExpected(funcName string, paramValue any, paramPos, level, subPos int) (err error) { | ||||
| 	if !(IsNumber(paramValue) || isFraction(paramValue)) /*|| isList(paramValue)*/ { | ||||
| 	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) | ||||
| 	} | ||||
| @ -45,7 +45,7 @@ func doAdd(ctx ExprContext, name string, it Iterator, count, level int) (result | ||||
| 		count++ | ||||
| 
 | ||||
| 		if !sumAsFloat { | ||||
| 			if IsFloat(v) { | ||||
| 			if isFloat(v) { | ||||
| 				sumAsFloat = true | ||||
| 				if sumAsFract { | ||||
| 					floatSum = fractSum.toFloat() | ||||
| @ -120,7 +120,7 @@ func doMul(ctx ExprContext, name string, it Iterator, count, level int) (result | ||||
| 		count++ | ||||
| 
 | ||||
| 		if !mulAsFloat { | ||||
| 			if IsFloat(v) { | ||||
| 			if isFloat(v) { | ||||
| 				mulAsFloat = true | ||||
| 				if mulAsFract { | ||||
| 					floatProd = fractProd.toFloat() | ||||
|  | ||||
| @ -26,7 +26,7 @@ func evalBuiltin(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	} | ||||
| 
 | ||||
| 	count := 0 | ||||
| 	if IsString(childValue) { | ||||
| 	if isString(childValue) { | ||||
| 		module, _ := childValue.(string) | ||||
| 		count, err = ImportInContextByGlobPattern(ctx, module) | ||||
| 	} else { | ||||
|  | ||||
| @ -25,7 +25,7 @@ func evalFact(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsInteger(leftValue) { | ||||
| 	if isInteger(leftValue) { | ||||
| 		if i, _ := leftValue.(int64); i >= 0 { | ||||
| 			f := int64(1) | ||||
| 			for k := int64(1); k <= i; k++ { | ||||
|  | ||||
| @ -24,7 +24,7 @@ func evalInclude(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	} | ||||
| 
 | ||||
| 	count := 0 | ||||
| 	if IsList(childValue) { | ||||
| 	if isList(childValue) { | ||||
| 		list, _ := childValue.([]any) | ||||
| 		for i, filePathSpec := range list { | ||||
| 			if filePath, ok := filePathSpec.(string); ok { | ||||
| @ -39,7 +39,7 @@ func evalInclude(ctx ExprContext, self *term) (v any, err error) { | ||||
| 				break | ||||
| 			} | ||||
| 		} | ||||
| 	} else if IsString(childValue) { | ||||
| 	} else if isString(childValue) { | ||||
| 		filePath, _ := childValue.(string) | ||||
| 		v, err = EvalFile(ctx, filePath) | ||||
| 	} else { | ||||
|  | ||||
| @ -33,7 +33,7 @@ func evalInsert(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsList(rightValue) { | ||||
| 	if isList(rightValue) { | ||||
| 		list, _ := rightValue.(*ListType) | ||||
| 		newList := append(ListType{leftValue}, *list...) | ||||
| 		v = &newList | ||||
| @ -50,7 +50,7 @@ func evalAppend(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsList(leftValue) { | ||||
| 	if isList(leftValue) { | ||||
| 		list, _ := leftValue.(*ListType) | ||||
| 		newList := append(*list, rightValue) | ||||
| 		v = &newList | ||||
|  | ||||
| @ -23,10 +23,10 @@ func evalLength(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsList(childValue) { | ||||
| 	if isList(childValue) { | ||||
| 		list, _ := childValue.([]any) | ||||
| 		v = int64(len(list)) | ||||
| 	} else if IsString(childValue) { | ||||
| 	} else if isString(childValue) { | ||||
| 		s, _ := childValue.(string) | ||||
| 		v = int64(len(s)) | ||||
| 	} else if it, ok := childValue.(Iterator); ok { | ||||
|  | ||||
| @ -25,7 +25,7 @@ func evalPostInc(ctx ExprContext, self *term) (v any, err error) { | ||||
| 
 | ||||
| 	if it, ok := childValue.(Iterator); ok { | ||||
| 		v, err = it.Next() | ||||
| 	} else if IsInteger(childValue) && self.children[0].symbol() == SymIdentifier { | ||||
| 	} else if isInteger(childValue) && self.children[0].symbol() == SymIdentifier { | ||||
| 		v = childValue | ||||
| 		i, _ := childValue.(int64) | ||||
| 		ctx.SetVar(self.children[0].source(), i+1) | ||||
|  | ||||
| @ -30,12 +30,12 @@ func evalMultiply(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsString(leftValue) && IsInteger(rightValue) { | ||||
| 	if isString(leftValue) && isInteger(rightValue) { | ||||
| 		s, _ := leftValue.(string) | ||||
| 		n, _ := rightValue.(int64) | ||||
| 		v = strings.Repeat(s, int(n)) | ||||
| 	} else if isNumOrFract(leftValue) && isNumOrFract(rightValue) { | ||||
| 		if IsFloat(leftValue) || IsFloat(rightValue) { | ||||
| 		if isFloat(leftValue) || isFloat(rightValue) { | ||||
| 			v = numAsFloat(leftValue) * numAsFloat(rightValue) | ||||
| 		} else if isFraction(leftValue) || isFraction(rightValue) { | ||||
| 			v, err = mulAnyFract(leftValue, rightValue) | ||||
| @ -72,7 +72,7 @@ func evalDivide(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	} | ||||
| 
 | ||||
| 	if isNumOrFract(leftValue) && isNumOrFract(rightValue) { | ||||
| 		if IsFloat(leftValue) || IsFloat(rightValue) { | ||||
| 		if isFloat(leftValue) || isFloat(rightValue) { | ||||
| 			d := numAsFloat(rightValue) | ||||
| 			if d == 0.0 { | ||||
| 				err = errors.New("division by zero") | ||||
| @ -148,7 +148,7 @@ func evalReminder(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsInteger(leftValue) && IsInteger(rightValue) { | ||||
| 	if isInteger(leftValue) && isInteger(rightValue) { | ||||
| 		rightInt, _ := rightValue.(int64) | ||||
| 		if rightInt == 0 { | ||||
| 			err = errors.New("division by zero") | ||||
|  | ||||
| @ -25,15 +25,15 @@ func evalEqual(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsNumber(leftValue) && IsNumber(rightValue) { | ||||
| 		if IsInteger(leftValue) && IsInteger(rightValue) { | ||||
| 	if isNumber(leftValue) && isNumber(rightValue) { | ||||
| 		if isInteger(leftValue) && isInteger(rightValue) { | ||||
| 			li, _ := leftValue.(int64) | ||||
| 			ri, _ := rightValue.(int64) | ||||
| 			v = li == ri | ||||
| 		} else { | ||||
| 			v = numAsFloat(leftValue) == numAsFloat(rightValue) | ||||
| 		} | ||||
| 	} else if IsString(leftValue) && IsString(rightValue) { | ||||
| 	} else if isString(leftValue) && isString(rightValue) { | ||||
| 		ls, _ := leftValue.(string) | ||||
| 		rs, _ := rightValue.(string) | ||||
| 		v = ls == rs | ||||
| @ -111,15 +111,15 @@ func evalLess(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsNumber(leftValue) && IsNumber(rightValue) { | ||||
| 		if IsInteger(leftValue) && IsInteger(rightValue) { | ||||
| 	if isNumber(leftValue) && isNumber(rightValue) { | ||||
| 		if isInteger(leftValue) && isInteger(rightValue) { | ||||
| 			li, _ := leftValue.(int64) | ||||
| 			ri, _ := rightValue.(int64) | ||||
| 			v = li < ri | ||||
| 		} else { | ||||
| 			v = numAsFloat(leftValue) < numAsFloat(rightValue) | ||||
| 		} | ||||
| 	} else if IsString(leftValue) && IsString(rightValue) { | ||||
| 	} else if isString(leftValue) && isString(rightValue) { | ||||
| 		ls, _ := leftValue.(string) | ||||
| 		rs, _ := rightValue.(string) | ||||
| 		v = ls < rs | ||||
| @ -148,15 +148,15 @@ func evalLessEqual(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsNumber(leftValue) && IsNumber(rightValue) { | ||||
| 		if IsInteger(leftValue) && IsInteger(rightValue) { | ||||
| 	if isNumber(leftValue) && isNumber(rightValue) { | ||||
| 		if isInteger(leftValue) && isInteger(rightValue) { | ||||
| 			li, _ := leftValue.(int64) | ||||
| 			ri, _ := rightValue.(int64) | ||||
| 			v = li <= ri | ||||
| 		} else { | ||||
| 			v = numAsFloat(leftValue) <= numAsFloat(rightValue) | ||||
| 		} | ||||
| 	} else if IsString(leftValue) && IsString(rightValue) { | ||||
| 	} else if isString(leftValue) && isString(rightValue) { | ||||
| 		ls, _ := leftValue.(string) | ||||
| 		rs, _ := rightValue.(string) | ||||
| 		v = ls <= rs | ||||
|  | ||||
| @ -35,14 +35,14 @@ func evalSign(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if IsFloat(rightValue) { | ||||
| 	if isFloat(rightValue) { | ||||
| 		if self.tk.Sym == SymChangeSign { | ||||
| 			f, _ := rightValue.(float64) | ||||
| 			v = -f | ||||
| 		} else { | ||||
| 			v = rightValue | ||||
| 		} | ||||
| 	} else if IsInteger(rightValue) { | ||||
| 	} else if isInteger(rightValue) { | ||||
| 		if self.tk.Sym == SymChangeSign { | ||||
| 			i, _ := rightValue.(int64) | ||||
| 			v = -i | ||||
|  | ||||
| @ -28,17 +28,17 @@ func evalPlus(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if (IsString(leftValue) && isNumberString(rightValue)) || (IsString(rightValue) && isNumberString(leftValue)) { | ||||
| 	if (isString(leftValue) && isNumberString(rightValue)) || (isString(rightValue) && isNumberString(leftValue)) { | ||||
| 		v = fmt.Sprintf("%v%v", leftValue, rightValue) | ||||
| 	} else if IsNumber(leftValue) && IsNumber(rightValue) { | ||||
| 		if IsFloat(leftValue) || IsFloat(rightValue) { | ||||
| 	} else if isNumber(leftValue) && isNumber(rightValue) { | ||||
| 		if isFloat(leftValue) || isFloat(rightValue) { | ||||
| 			v = numAsFloat(leftValue) + numAsFloat(rightValue) | ||||
| 		} else { | ||||
| 			leftInt, _ := leftValue.(int64) | ||||
| 			rightInt, _ := rightValue.(int64) | ||||
| 			v = leftInt + rightInt | ||||
| 		} | ||||
| 	} else if IsList(leftValue) || IsList(rightValue) { | ||||
| 	} else if isList(leftValue) || isList(rightValue) { | ||||
| 		var leftList, rightList *ListType | ||||
| 		var ok bool | ||||
| 		if leftList, ok = leftValue.(*ListType); !ok { | ||||
| @ -56,7 +56,7 @@ func evalPlus(ctx ExprContext, self *term) (v any, err error) { | ||||
| 		} | ||||
| 		v = &sumList | ||||
| 	} else if isFraction(leftValue) || isFraction(rightValue) { | ||||
| 		if IsFloat(leftValue) || IsFloat(rightValue) { | ||||
| 		if isFloat(leftValue) || isFloat(rightValue) { | ||||
| 			v = numAsFloat(leftValue) + numAsFloat(rightValue) | ||||
| 		} else { | ||||
| 			v, err = sumAnyFract(leftValue, rightValue) | ||||
| @ -87,7 +87,7 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) { | ||||
| 	} | ||||
| 
 | ||||
| 	if isNumOrFract(leftValue) && isNumOrFract(rightValue) { | ||||
| 		if IsFloat(leftValue) || IsFloat(rightValue) { | ||||
| 		if isFloat(leftValue) || isFloat(rightValue) { | ||||
| 			v = numAsFloat(leftValue) - numAsFloat(rightValue) | ||||
| 		} else if isFraction(leftValue) || isFraction(rightValue) { | ||||
| 			v, err = subAnyFract(leftValue, rightValue) | ||||
| @ -96,7 +96,7 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) { | ||||
| 			rightInt, _ := rightValue.(int64) | ||||
| 			v = leftInt - rightInt | ||||
| 		} | ||||
| 	} else if IsList(leftValue) && IsList(rightValue) { | ||||
| 	} else if isList(leftValue) && isList(rightValue) { | ||||
| 		leftList, _ := leftValue.(*ListType) | ||||
| 		rightList, _ := rightValue.(*ListType) | ||||
| 		diffList := make(ListType, 0, len(*leftList)-len(*rightList)) | ||||
|  | ||||
							
								
								
									
										18
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								utils.go
									
									
									
									
									
								
							| @ -9,41 +9,41 @@ import ( | ||||
| 	"reflect" | ||||
| ) | ||||
| 
 | ||||
| func IsString(v any) (ok bool) { | ||||
| func isString(v any) (ok bool) { | ||||
| 	_, ok = v.(string) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func IsInteger(v any) (ok bool) { | ||||
| func isInteger(v any) (ok bool) { | ||||
| 	_, ok = v.(int64) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func IsFloat(v any) (ok bool) { | ||||
| func isFloat(v any) (ok bool) { | ||||
| 	_, ok = v.(float64) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func IsList(v any) (ok bool) { | ||||
| func isList(v any) (ok bool) { | ||||
| 	_, ok = v.(*ListType) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func IsDict(v any) (ok bool) { | ||||
| func isDict(v any) (ok bool) { | ||||
| 	_, ok = v.(map[any]any) | ||||
| 	return ok | ||||
| } | ||||
| 
 | ||||
| func IsNumber(v any) (ok bool) { | ||||
| 	return IsFloat(v) || IsInteger(v) | ||||
| func isNumber(v any) (ok bool) { | ||||
| 	return isFloat(v) || isInteger(v) | ||||
| } | ||||
| 
 | ||||
| func isNumOrFract(v any) (ok bool) { | ||||
| 	return IsFloat(v) || IsInteger(v) || isFraction(v) | ||||
| 	return isFloat(v) || isInteger(v) || isFraction(v) | ||||
| } | ||||
| 
 | ||||
| func isNumberString(v any) (ok bool) { | ||||
| 	return IsString(v) || IsNumber(v) | ||||
| 	return isString(v) || isNumber(v) | ||||
| } | ||||
| 
 | ||||
| func isFunctor(v any) (ok bool) { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user