Compare commits

...

25 Commits

Author SHA1 Message Date
camoroso 3b641ac793 Doc: little changes 2024-06-21 09:06:25 +02:00
camoroso a1a62b6794 Doc: little changes 2024-06-20 07:10:59 +02:00
camoroso a18d92534f Doc: little changes 2024-06-19 22:51:37 +02:00
camoroso ec0963e26f Doc: little changes 2024-06-19 22:46:35 +02:00
camoroso be992131b1 Doc: little changes 2024-06-19 22:25:38 +02:00
camoroso 0e3960321f Doc: little changes 2024-06-19 22:22:10 +02:00
camoroso 61d34fef7d Doc: little changes 2024-06-19 22:20:28 +02:00
camoroso 581f1585e6 Doc: embedded images 2024-06-19 22:04:13 +02:00
camoroso 531cb1c249 Doc: concepts 2024-06-19 09:38:02 +02:00
camoroso d1b468f35b t_list_test.go: new test cases added 2024-06-19 09:24:50 +02:00
camoroso ff9cf80c66 operator-index.go: ConstLastIndex is checked 2024-06-19 09:24:19 +02:00
camoroso d63b18fd76 parser.go: SymColon resets the firstToken flag; that is needed to specify range indeces that can define negative limits 2024-06-19 09:22:40 +02:00
camoroso 019470faf1 operator-range.go: Fixed priority bug; when range only has the left limit, right limit is set to ConstLastIndex constant 2024-06-19 09:20:02 +02:00
camoroso 302430d57d common-params.go: added the constant ConstLastIndex 2024-06-19 09:17:46 +02:00
camoroso 62ef0d699d token.go: new function IsOneOf() 2024-06-19 09:16:19 +02:00
camoroso 866de759dd file-reader.expr: simpler implementation 2024-06-17 14:07:39 +02:00
camoroso b1d6b6de44 refactored dict's item access 2024-06-17 14:06:33 +02:00
camoroso 7e357eea62 changed to comply new builtin-os-file.go's function names 2024-06-17 09:54:20 +02:00
camoroso d6b4c79736 operator-index.go: dict item access by generic keys implemented 2024-06-17 09:05:23 +02:00
camoroso d066344af8 builtin-os-file.go: changed read and write function names; added fileReadTextAll 2024-06-17 06:59:15 +02:00
camoroso f41dba069e plugin.go: support Liteide and VScode to debug executable file name 2024-06-17 06:57:47 +02:00
camoroso 703ecf6829 Added utility function GetLast() to context 2024-06-17 06:56:32 +02:00
camoroso ba479a1b99 Function call moved from operand-func.go to function.go 2024-06-17 06:54:50 +02:00
camoroso 24e6a293b0 common-params.go: ParamName added 2024-06-12 11:16:57 +02:00
camoroso 28f464c4dc plugins.go: If the main program's file name ends with '.debug', plugins will be loaded from file with name ending with '.debug' 2024-06-12 11:15:31 +02:00
22 changed files with 1024 additions and 442 deletions
+12 -12
View File
@@ -43,16 +43,16 @@ func (functor *exprFunctor) Invoke(ctx ExprContext, name string, args []any) (re
return return
} }
func CallExprFunction(parentCtx ExprContext, funcName string, params ...any) (v any, err error) { // func CallExprFunction(parentCtx ExprContext, funcName string, params ...any) (v any, err error) {
ctx := cloneContext(parentCtx) // ctx := cloneContext(parentCtx)
ctx.SetParent(parentCtx) // ctx.SetParent(parentCtx)
if err == nil { // if err == nil {
if err = checkFunctionCall(ctx, funcName, &params); err == nil { // if err = checkFunctionCall(ctx, funcName, &params); err == nil {
if v, err = ctx.Call(funcName, params); err == nil { // if v, err = ctx.Call(funcName, params); err == nil {
exportObjects(parentCtx, ctx) // exportObjects(parentCtx, ctx)
} // }
} // }
} // }
return // return
} // }
+52 -16
View File
@@ -68,7 +68,7 @@ func createFileFunc(ctx ExprContext, name string, args []any) (result any, err e
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)} result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
} }
} else { } else {
err = errMissingFilePath("createFile") err = errMissingFilePath(name)
} }
return return
} }
@@ -80,7 +80,7 @@ func openFileFunc(ctx ExprContext, name string, args []any) (result any, err err
result = &osReader{fh: fh, reader: bufio.NewReader(fh)} result = &osReader{fh: fh, reader: bufio.NewReader(fh)}
} }
} else { } else {
err = errMissingFilePath("openFile") err = errMissingFilePath(name)
} }
return return
} }
@@ -92,7 +92,7 @@ func appendFileFunc(ctx ExprContext, name string, args []any) (result any, err e
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)} result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
} }
} else { } else {
err = errMissingFilePath("appendFile") err = errMissingFilePath(name)
} }
return return
} }
@@ -118,13 +118,13 @@ func closeFileFunc(ctx ExprContext, name string, args []any) (result any, err er
} }
} }
if err == nil && (handle == nil || invalidFileHandle != nil) { if err == nil && (handle == nil || invalidFileHandle != nil) {
err = errInvalidFileHandle("closeFileFunc", handle) err = errInvalidFileHandle(name, handle)
} }
result = err == nil result = err == nil
return return
} }
func writeFileFunc(ctx ExprContext, name string, args []any) (result any, err error) { func fileWriteTextFunc(ctx ExprContext, name string, args []any) (result any, err error) {
var handle osHandle var handle osHandle
var invalidFileHandle any var invalidFileHandle any
var ok bool var ok bool
@@ -142,12 +142,12 @@ func writeFileFunc(ctx ExprContext, name string, args []any) (result any, err er
} }
if err == nil && (handle == nil || invalidFileHandle != nil) { if err == nil && (handle == nil || invalidFileHandle != nil) {
err = errInvalidFileHandle("writeFileFunc", invalidFileHandle) err = errInvalidFileHandle(name, invalidFileHandle)
} }
return return
} }
func readFileFunc(ctx ExprContext, name string, args []any) (result any, err error) { func fileReadTextFunc(ctx ExprContext, name string, args []any) (result any, err error) {
var handle osHandle var handle osHandle
var invalidFileHandle any var invalidFileHandle any
var ok bool var ok bool
@@ -165,23 +165,50 @@ func readFileFunc(ctx ExprContext, name string, args []any) (result any, err err
limit = s[0] limit = s[0]
} }
if v, err = r.reader.ReadString(limit); err == nil { v, err = r.reader.ReadString(limit)
if len(v) > 0 && v[len(v)-1] == limit { if err == io.EOF {
err = nil
}
if len(v) > 0 {
if v[len(v)-1] == limit {
result = v[0 : len(v)-1] result = v[0 : len(v)-1]
} else { } else {
result = v result = v
} }
} }
if err == io.EOF {
err = nil
}
} else { } else {
invalidFileHandle = handle invalidFileHandle = handle
} }
} }
if err == nil && (handle == nil || invalidFileHandle != nil) { if err == nil && (handle == nil || invalidFileHandle != nil) {
err = errInvalidFileHandle("readFileFunc", invalidFileHandle) err = errInvalidFileHandle(name, invalidFileHandle)
}
return
}
func fileReadTextAllFunc(ctx ExprContext, name string, args []any) (result any, err error) {
var handle osHandle
var invalidFileHandle any
var ok bool
result = nil
if handle, ok = args[0].(osHandle); !ok || args[0] == nil {
invalidFileHandle = args[0]
}
if handle != nil {
if r, ok := handle.(*osReader); ok {
var b []byte
b, err = io.ReadAll(r.reader)
result = string(b)
} else {
invalidFileHandle = handle
}
}
if err == nil && (handle == nil || invalidFileHandle != nil) {
err = errInvalidFileHandle(name, invalidFileHandle)
} }
return return
} }
@@ -190,21 +217,30 @@ func ImportOsFuncs(ctx ExprContext) {
ctx.RegisterFunc("fileOpen", NewGolangFunctor(openFileFunc), TypeHandle, []ExprFuncParam{ ctx.RegisterFunc("fileOpen", NewGolangFunctor(openFileFunc), TypeHandle, []ExprFuncParam{
NewFuncParam(ParamFilepath), NewFuncParam(ParamFilepath),
}) })
ctx.RegisterFunc("fileAppend", NewGolangFunctor(appendFileFunc), TypeHandle, []ExprFuncParam{ ctx.RegisterFunc("fileAppend", NewGolangFunctor(appendFileFunc), TypeHandle, []ExprFuncParam{
NewFuncParam(ParamFilepath), NewFuncParam(ParamFilepath),
}) })
ctx.RegisterFunc("fileCreate", NewGolangFunctor(createFileFunc), TypeHandle, []ExprFuncParam{ ctx.RegisterFunc("fileCreate", NewGolangFunctor(createFileFunc), TypeHandle, []ExprFuncParam{
NewFuncParam(ParamFilepath), NewFuncParam(ParamFilepath),
}) })
ctx.RegisterFunc("fileWrite", NewGolangFunctor(writeFileFunc), TypeInt, []ExprFuncParam{
ctx.RegisterFunc("fileClose", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{
NewFuncParam(TypeHandle),
})
ctx.RegisterFunc("fileWriteText", NewGolangFunctor(fileWriteTextFunc), TypeInt, []ExprFuncParam{
NewFuncParam(TypeHandle), NewFuncParam(TypeHandle),
NewFuncParamFlagDef(TypeItem, PfDefault|PfRepeat, ""), NewFuncParamFlagDef(TypeItem, PfDefault|PfRepeat, ""),
}) })
ctx.RegisterFunc("fileRead", NewGolangFunctor(readFileFunc), TypeString, []ExprFuncParam{
ctx.RegisterFunc("fileReadText", NewGolangFunctor(fileReadTextFunc), TypeString, []ExprFuncParam{
NewFuncParam(TypeHandle), NewFuncParam(TypeHandle),
NewFuncParamFlagDef("limitCh", PfDefault, "\n"), NewFuncParamFlagDef("limitCh", PfDefault, "\n"),
}) })
ctx.RegisterFunc("fileClose", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{
ctx.RegisterFunc("fileReadTextAll", NewGolangFunctor(fileReadTextAllFunc), TypeString, []ExprFuncParam{
NewFuncParam(TypeHandle), NewFuncParam(TypeHandle),
}) })
} }
+6
View File
@@ -15,7 +15,13 @@ const (
ParamStart = "start" ParamStart = "start"
ParamEnd = "end" ParamEnd = "end"
ParamValue = "value" ParamValue = "value"
ParamName = "name"
ParamEllipsis = "..." ParamEllipsis = "..."
ParamFilepath = "filepath" ParamFilepath = "filepath"
ParamDirpath = "dirpath" ParamDirpath = "dirpath"
) )
// to be moved in its own source file
const (
ConstLastIndex = 0xFFFF_FFFF
)
+6
View File
@@ -41,3 +41,9 @@ func exportObjects(destCtx, sourceCtx ExprContext) {
} }
} }
} }
func exportObjectsToParent(sourceCtx ExprContext) {
if parentCtx := sourceCtx.GetParent(); parentCtx != nil {
exportObjects(parentCtx, sourceCtx)
}
}
+1
View File
@@ -40,6 +40,7 @@ type ExprContext interface {
SetParent(ctx ExprContext) SetParent(ctx ExprContext)
GetParent() (ctx ExprContext) GetParent() (ctx ExprContext)
GetVar(varName string) (value any, exists bool) GetVar(varName string) (value any, exists bool)
GetLast() any
SetVar(varName string, value any) SetVar(varName string, value any)
UnsafeSetVar(varName string, value any) UnsafeSetVar(varName string, value any)
EnumVars(func(name string) (accept bool)) (varNames []string) EnumVars(func(name string) (accept bool)) (varNames []string)
+282 -135
View File
@@ -9,6 +9,7 @@ Expressions calculator
:icons: font :icons: font
:icon-set: fi :icon-set: fi
:numbered: :numbered:
:data-uri:
//:table-caption: Tabella //:table-caption: Tabella
//:figure-caption: Diagramma //:figure-caption: Diagramma
:docinfo1: :docinfo1:
@@ -19,21 +20,52 @@ Expressions calculator
:rouge-style: gruvbox :rouge-style: gruvbox
// :rouge-style: colorful // :rouge-style: colorful
//:rouge-style: monokay //:rouge-style: monokay
// Work around to manage double-column in back-tick quotes
:2c: ::
toc::[] toc::[]
#TODO: Work in progress (last update on 2024/06/02, 08:18 a.m.)# #TODO: Work in progress (last update on 2024/06/21, 05:40 a.m.)#
== Expr == Expr
_Expr_ is a GO package capable of analysing, interpreting and calculating expressions. _Expr_ is a GO package capable of analysing, interpreting and calculating expressions.
=== Concepts and terminology === Concepts and terminology
#TODO#
Expressions are texts containing sequences of operations represented by a syntax very similar to that of most programming languages. _Expr_ package provides these macro functions:
* *_Scanner_* -- Its input is a text. It scans expression text characters to produce a flow of logical symbol and related attributes, aka tokens.
* *_Parser_* -- Parser input is the token flow coming from the scanner. It analyses the token flow verifyng if it complies with the _Expr_ syntax. If that is the case, the Parser generates the Abstract Syntax Tree (AST). This is tree data structure that represents the components of an expressions and how they are related one each other.
* *_Calculator_*. Its input is the AST. It computes the parsed expression contained in the AST and returns the result or an error.
image::expression-diagram.png[] image::expression-diagram.png[]
==== Variables
_Expr_ supports variables. The result of an expression can be stored in a variable and reused in other espressions simply specifying the name of the variable as an operand.
==== Multi-expression
An input text valid for _Expr_ can contain more than an expression. Expressions are separated by [blue]`;` (semicolon). When an input contains two or more expressions it is called _multi-expression_.
_Expr_ parses and computes each expression of a multi-espression, from the left to the right. If all expressions are computed without errors, it only returns the value of the last, the right most.
The result of each expression of a multi-expression is stored in an automatic variable named _last_. In this way, each expression can refer to the result of the previous one without the need to assign that value to a new dedicated variable.
==== Calculation context
All objects, such as variables and functions, created during the calculation of an expression are stored in a memory called _context_.
The expression context is analogous to the stack-frame of other programming languages. When a function is called, a new context is allocated to store local definitions.
Function contexts are created by cloning the calling context. More details on this topic are given later in this document.
_Expr_ creates and keeps a inner _global context_ where it stores imported functions, either from builtin or plugin modules. To perform calculations, the calling program must provide its own context; this is the _main context_. All calculations take place in this context. As mentioned eralier, when a function is called, a new context is created by cloning the calling context. The createt context can be called _function context_.
Imported functions are registerd in the _global context_. When an expression first calls an imported function, that function is linked to the current context; this can be the _main context_ or a _function context_.
=== `dev-expr` test tool === `dev-expr` test tool
`dev-expr` is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, in additon to the automatic verification test suite based on the Go test framework, `dev-expr` provides an important aid for quickly testing of new features during their development. Before we begin to describe the syntax of _Expr_, it is worth introducing _dev-expr_ because it will be used to show many examples of expressions.
`dev-expr` is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, in additon to the automatic verification test suite based on the Go test framework, `dev-expr` provided an important aid for quickly testing of new features during their development.
`dev-expr` can work as a _REPL_, _**R**ead-**E**xecute-**P**rint-**L**oop_, or it can process expression acquired from files or standard input. `dev-expr` can work as a _REPL_, _**R**ead-**E**xecute-**P**rint-**L**oop_, or it can process expression acquired from files or standard input.
@@ -47,11 +79,10 @@ Here are some examples of execution.
# Type 'exit' or Ctrl+D to quit the program. # Type 'exit' or Ctrl+D to quit the program.
[user]$ ./dev-expr [user]$ ./dev-expr
expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it) dev-expr -- Expressions calculator v1.10.0(build 14),2024/06/17 (celestino.amoroso@portale-stac.it)
Based on the Expr package v0.10.0 Based on the Expr package v0.19.0
Type help to get the list of available commands Type help to get the list of available commands
See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
>>> help >>> help
--- REPL commands: --- REPL commands:
source -- Load a file as input source -- Load a file as input
@@ -61,6 +92,7 @@ expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@por
help -- Show command list help -- Show command list
ml -- Enable/Disable multi-line output ml -- Enable/Disable multi-line output
mods -- List builtin modules mods -- List builtin modules
output -- Enable/Disable printing expression results. Options 'on', 'off', 'status'
--- Command line options: --- Command line options:
-b <builtin> Import builtin modules. -b <builtin> Import builtin modules.
@@ -70,9 +102,10 @@ expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@por
-i Force REPL operation when all -e occurences have been processed -i Force REPL operation when all -e occurences have been processed
-h, --help, help Show this help menu -h, --help, help Show this help menu
-m, --modules List all builtin modules -m, --modules List all builtin modules
--noout Disable printing of expression results
-p Print prefix form -p Print prefix form
-t Print tree form <2> -t Print tree form <2>
-v, --version Show program version
>>> >>>
---- ----
@@ -83,8 +116,8 @@ expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@por
[source,shell] [source,shell]
---- ----
[user]$ ./dev-expr [user]$ ./dev-expr
expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it) dev-expr -- Expressions calculator v1.10.0(build 14),2024/06/17 (celestino.amoroso@portale-stac.it)
Based on the Expr package v0.10.0 Based on the Expr package v0.19.0
Type help to get the list of available commands Type help to get the list of available commands
See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
@@ -101,21 +134,21 @@ expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@por
7 7
- -
6 6
>>> 1+2 but 5|2+0.5 <4> >>> 4+2 but 5|2+0.5 <4>
3 3
>>> 1+2; 5|2+0.5 <5> >>> 4+2; 5|2+0.5 <5>
3 3
>>> >>>
---- ----
<1> Number bases: 0x = hexadecimal, 0o = octal, 0b = binary. <1> Number bases: 0x = _hexadecimal_, 0o = _octal_, 0b = _binary_.
<2> Fractions: numerator | denominator. <2> Fractions: _numerator_ | _denominator_.
<3> Activate multi-line output of fractions. <3> Activate multi-line output of fractions.
<4> But operator, see <<_but_operator>>. <4> But operator, see <<_but_operator>>.
<5> Multi-expression: the same result of the previous single expression but this it is obtained with two separated calculations. <5> Multi-expression: the same result of the previous single expression but this it is obtained with two separated calculations.
== Data types == Data types
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists. _Expr_ has its type system which is a subset of Golang's type system. It supports numerical, string, relational, boolean expressions, and mixed-type lists and maps.
=== Numbers === Numbers
_Expr_ supports three type of numbers: _Expr_ supports three type of numbers:
@@ -173,15 +206,20 @@ _dec-seq_ = _see-integer-literal-syntax_
.Examples .Examples
`>>>` [blue]`1.0` + `>>>` [blue]`1.0` +
[green]`1` + [green]`1`
`>>>` [blue]`0.123` + `>>>` [blue]`0.123` +
[green]`0.123` + [green]`0.123`
`>>>` [blue]`4.5e+3` + `>>>` [blue]`4.5e+3` +
[green]`4500` + [green]`4500`
`>>>` [blue]`4.5E-33` + `>>>` [blue]`4.5E-33` +
[green]`4.5e-33` + [green]`4.5e-33`
`>>>` [blue]`4.5E-3` + `>>>` [blue]`4.5E-3` +
[green]`0.0045` + [green]`0.0045`
`>>>` [blue]`4.5E10` + `>>>` [blue]`4.5E10` +
[green]`4.5e+10` [green]`4.5e+10`
@@ -211,35 +249,43 @@ _digit-seq_ = _see-integer-literal-syntax_
==== ====
.Examples .Examples
// [source,go]
// ----
`>>>` [blue]`1 | 2` + `>>>` [blue]`1 | 2` +
[green]`1|2` + [green]`1|2`
`>>>` [blue]`4|6` [gray]_// Fractions are always reduced to their lowest terms_ + `>>>` [blue]`4|6` [gray]_// Fractions are always reduced to their lowest terms_ +
[green]`2|3` + [green]`2|3`
`>>>` [blue]`1|2 + 2|3` + `>>>` [blue]`1|2 + 2|3` +
[green]`7|6` + [green]`7|6`
`>>>` [blue]`1|2 * 2|3` + `>>>` [blue]`1|2 * 2|3` +
[green]`1|3` + [green]`1|3`
`>>>` [blue]`1|2 / 1|3` + `>>>` [blue]`1|2 / 1|3` +
[green]`3|2` + [green]`3|2`
`>>>` [blue]`1|2 ./ 1|3` [gray]_// Force decimal division_ + `>>>` [blue]`1|2 ./ 1|3` [gray]_// Force decimal division_ +
[green]`1.5` + [green]`1.5`
`>>>` [blue]`-1|2` + `>>>` [blue]`-1|2` +
[green]`-1|2` + [green]`-1|2`
`>>>` [blue]`1|-2` [gray]_// Invalid sign specification_ + `>>>` [blue]`1|-2` [gray]_// Invalid sign specification_ +
[red]_Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1_ + [red]_Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1_
`>>>` [blue]`1|(-2)` + `>>>` [blue]`1|(-2)` +
[green]`-1|2` [green]`-1|2`
// ----
Fractions can be used together with integers and floats in expressions. Fractions can be used together with integers and floats in expressions.
.Examples .Examples
`>>>` [blue]`1|2 + 5` + `>>>` [blue]`1|2 + 5` +
[green]`11|2` + [green]`11|2`
`>>>` [blue]`4 - 1|2` + `>>>` [blue]`4 - 1|2` +
[green]`7|2` + [green]`7|2`
`>>>` [blue]`1.0 + 1|2` + `>>>` [blue]`1.0 + 1|2` +
[green]`1.5` [green]`1.5`
@@ -250,12 +296,15 @@ Strings are character sequences enclosed between two double quote [blue]`"`.
.Examples .Examples
`>>>` [blue]`"I'm a string"` + `>>>` [blue]`"I'm a string"` +
[green]`I'm a string` + [green]`I'm a string`
`>>>` [blue]`"123abc?!"` + `>>>` [blue]`"123abc?!"` +
[green]`123abc?!` + [green]`123abc?!`
`>>>` [blue]`"123\nabc"` + `>>>` [blue]`"123\nabc"` +
[green]`123` + [green]`123` +
[green]`abc` + [green]`abc`
`>>>` [blue]`"123\tabc"` + `>>>` [blue]`"123\tabc"` +
[green]`123{nbsp}{nbsp}{nbsp}{nbsp}abc` [green]`123{nbsp}{nbsp}{nbsp}{nbsp}abc`
@@ -272,25 +321,36 @@ Some arithmetic operators can also be used with strings.
| [blue]`*` | _repeat_ | Make _n_ copy of a string | [blue]`"one" * 2` -> _"oneone"_ | [blue]`*` | _repeat_ | Make _n_ copy of a string | [blue]`"one" * 2` -> _"oneone"_
|=== |===
The items of strings can be accessed using the dot `.` operator. The items of strings can be accessed using the square `[]` operator.
.Item access syntax .Item access syntax
==== ====
_item_ = _string-expr_ "**.**" _integer-expr_ *_item_* = _string-expr_ "**[**" _integer-expr_ "**]**"
====
.Sub-string syntax
====
*_sub-string_* = _string-expr_ "**[**" _integer-expr_ "**:**" _integer-expr_ "**]**"
==== ====
.String examples .String examples
`>>>` [blue]`s="abc"` [gray]_// assign the string to variable s_ + `>>>` [blue]`s="abcd"` [gray]_// assign the string to variable s_ +
[green]`abc` + [green]`"abcd"`
`>>>` [blue]`s.1` [gray]_// char at position 1 (starting from 0)_ +
[green]`b` + `>>>` [blue]`s[1]` [gray]_// char at position 1 (starting from 0)_ +
`>>>` [blue]`s.(-1)` [gray]_// char at position -1, the rightmost one_ + [green]`"b"`
[green]`c` +
`>>>` [blue]`s.[-1]` [gray]_// char at position -1, the rightmost one_ +
[green]`"d"`
`>>>` [blue]`\#s` [gray]_// number of chars_ + `>>>` [blue]`\#s` [gray]_// number of chars_ +
[gren]`3` + [gren]`4`
`>>>` [blue]`#"abc"` [gray]_// number of chars_ +
`>>>` [blue]`#"abc"` [gray]_// number of chars_ +
[green]`3` [green]`3`
`>>>` [blue]`s[1:3]` [gray]_// chars from position 1 to position 3 excluded_ +
[grean]`"bc"`
=== Booleans === Booleans
Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relational and boolean expressions result in boolean values. Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relational and boolean expressions result in boolean values.
@@ -334,14 +394,16 @@ Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relationa
[CAUTION] [CAUTION]
==== ====
Currently, boolean operations are evaluated using _short cut evaluation_. This means that, if the left expression of the [blue]`and` and [blue]`or` operators is sufficient to establish the result of the whole operation, the right expression would not evaluated at all. Currently, boolean operations are evaluated using _short cut evaluation_. This means that, if the left expression of the [blue]`and` and [blue]`or` operators is sufficient to establish the result of the whole operation, the right expression would not be evaluated at all.
.Example .Example
[source,go] [source,go]
---- ----
2 > (a=1) or (a=8) > 0; a // <1> 2 > (a=1) or (a=8) > 0; a // <1>
---- ----
<1> This multi-expression returns _1_ because in the first expression the left value of [blue]`or` is _true_ and as a conseguence its right value is not computed. Therefore the _a_ variable only receives the integer _1_. <1> This multi-expression returns _1_ because in the first expression the left value of [blue]`or` is _true_ and as a conseguence its right value is not computed. Therefore the _a_ variable only receives the integer _1_.
TIP: `dev-expr` provides the _ctrl()_ function that allows to change this behaviour.
==== ====
=== Lists === Lists
@@ -356,13 +418,17 @@ _non-empty-list_ = "**[**" _any-value_ {"**,**" _any-value} "**]**" +
.Examples .Examples
`>>>` [blue]`[1,2,3]` [gray]_// List of integers_ + `>>>` [blue]`[1,2,3]` [gray]_// List of integers_ +
[green]`[1, 2, 3]` + [green]`[1, 2, 3]`
`>>>` [blue]`["one", "two", "three"]` [gray]_// List of strings_ + `>>>` [blue]`["one", "two", "three"]` [gray]_// List of strings_ +
[green]`["one", "two", "three"]` + [green]`["one", "two", "three"]`
`>>>` [blue]`["one", 2, false, 4.1]` [gray]_// List of mixed-types_ + `>>>` [blue]`["one", 2, false, 4.1]` [gray]_// List of mixed-types_ +
[green]`["one", 2, false, 4.1]` + [green]`["one", 2, false, 4.1]`
`>>>` [blue]`["one"+1, 2.0*(9-2)]` [gray]_// List of expressions_ + `>>>` [blue]`["one"+1, 2.0*(9-2)]` [gray]_// List of expressions_ +
[green]`["one1", 14]` + [green]`["one1", 14]`
`>>>` [blue]`[ [1,"one"], [2,"two"]]` [gray]_// List of lists_ + `>>>` [blue]`[ [1,"one"], [2,"two"]]` [gray]_// List of lists_ +
[green]`[[1, "one"], [2, "two"]]` [green]`[[1, "one"], [2, "two"]]`
@@ -375,37 +441,52 @@ _non-empty-list_ = "**[**" _any-value_ {"**,**" _any-value} "**]**" +
| [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` -> _[1,3]_ | [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` -> _[1,3]_
| [blue]`>>` | _Front insertion_ | Insert an item in front | [blue]`0 >> [1,2]` -> _[0,1,2]_ | [blue]`>>` | _Front insertion_ | Insert an item in front | [blue]`0 >> [1,2]` -> _[0,1,2]_
| [blue]`<<` | _Back insertion_ | Insert an item at end | [blue]`[1,2] << 3` -> _[1,2,3]_ | [blue]`<<` | _Back insertion_ | Insert an item at end | [blue]`[1,2] << 3` -> _[1,2,3]_
| [blue]`.` | _List item_ | Item at given position | [blue]`[1,2.3].1` -> _2_ | [blue]`[]` | _Item at index_ | Item at given position | [blue]`[1,2,3][1]` -> _2_
| [blue]`in` | _Item in list_ | True if item is in list | [blue]`2 in [1,2,3]` -> _true_ + | [blue]`in` | _Item in list_ | True if item is in list | [blue]`2 in [1,2,3]` -> _true_ +
[blue]`6 in [1,2,3]` -> _false_ [blue]`6 in [1,2,3]` -> _false_
|=== |===
The items of array can be accessed using the dot `.` operator. Array's items can be accessed using the index `[]` operator.
.Item access syntax .Item access syntax
==== ====
_item_ = _list-expr_ "**.**" _integer-expr_ *_item_* = _list-expr_ "**[**" _integer-expr_ "**]**"
====
.Sub-array (or slice of array) syntax
====
*_slice_* = _string-expr_ "**[**" _integer-expr_ "**:**" _integer-expr_ "**]**"
==== ====
.Items of list .Items of list
`>>>` [blue]`[1,2,3].1` + `>>>` [blue]`[1,2,3].1` +
[green]`2` + [green]`2`
`>>>` [blue]`list=[1,2,3]; list.1` + `>>>` [blue]`list=[1,2,3]; list.1` +
[green]`2` + [green]`2`
`>>>` [blue]`["one","two","three"].1` + `>>>` [blue]`["one","two","three"].1` +
[green]`two` + [green]`two`
`>>>` [blue]`list=["one","two","three"]; list.(2-1)` + `>>>` [blue]`list=["one","two","three"]; list.(2-1)` +
[green]`two` + [green]`two`
`>>>` [blue]`list.(-1)` + `>>>` [blue]`list.(-1)` +
[green]`three` + [green]`three`
`>>>` [blue]`list.(10)` + `>>>` [blue]`list.(10)` +
[red]`Eval Error: [1:9] index 10 out of bounds` + [red]`Eval Error: [1:9] index 10 out of bounds`
`>>>` [blue]`#list` + `>>>` [blue]`#list` +
[green]`3` + [green]`3`
`>>>` [blue]`index=2; ["a", "b", "c", "d"].index` +
`>>>` [blue]`index=2; ["a", "b", "c", "d"][index]` +
[green]`c` [green]`c`
`>>>` [blue]`["a", "b", "c", "d"][2:]` +
[green]`["c", "d"]`
=== Dictionaries === Dictionaries
The _dictionary_, or _dict_, data-type is set of pairs _key/value_. It is also known as _map_ or _associative array_. The _dictionary_, or _dict_, data-type is set of pairs _key/value_. It is also known as _map_ or _associative array_.
@@ -419,14 +500,11 @@ _empty-dict_ = "**{}**" +
_non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar_ "**:**" _any-value} "**}**" + _non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar_ "**:**" _any-value} "**}**" +
==== ====
.Examples
`>>>` [blue]`{1:"one", 2:"two"}` +
[green]`{1: "one", 2: "two"}` +
`>>>` [blue]`{"one":1, "two": 2}` +
[green]`{"one": 1, "two": 2}` +
`>>>` [blue]`{"sum":1+2+3, "prod":1*2*3}` +
[green]`{"sum": 6, "prod": 6}`
.Item access syntax
====
*_item_* = _dict-expr_ "**[**" _key-expr_ "**]**"
====
.Dict operators .Dict operators
[cols="^2,^2,4,5"] [cols="^2,^2,4,5"]
@@ -434,11 +512,27 @@ _non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar
| Symbol | Operation | Description | Examples | Symbol | Operation | Description | Examples
| [blue]`+` | _Join_ | Joins two dicts | [blue]`{1:"one"}+{6:"six"}` -> _{1: "one", 6: "six"}_ | [blue]`+` | _Join_ | Joins two dicts | [blue]`{1:"one"}+{6:"six"}` -> _{1: "one", 6: "six"}_
| [blue]`.` | _Dict item value_ | Item value of given key | [blue]`{"one":1, "two":2}."two"` -> _2_ | [blue]`[]` | _Dict item value_ | Item value of given key | [blue]`{"one":1, "two":2}["two"]` -> _2_
| [blue]`in` | _Key in dict_ | True if key is in dict | [blue]`"one" in {"one":1, "two":2}` -> _true_ + | [blue]`in` | _Key in dict_ | True if key is in dict | [blue]`"one" in {"one":1, "two":2}` -> _true_ +
[blue]`"six" in {"one":1, "two":2}` -> _false_ [blue]`"six" in {"one":1, "two":2}` -> _false_
|=== |===
.Examples
`>>>` [blue]`{1:"one", 2:"two"}` +
[green]`{1: "one", 2: "two"}`
`>>>` [blue]`{"one":1, "two": 2}` +
[green]`{"one": 1, "two": 2}`
`>>>` [blue]`{"sum":1+2+3, "prod":1*2*3}` +
[green]`{"sum": 6, "prod": 6}`
`>>>` [blue]`{"one":1, "two":2}["two"]` +
[green]`2`
`>>>` [blue]`d={"one":1, "two":2}; d["six"]=6; d` +
[green]`{"two": 2, "one": 1, "six": 6}`
== Variables == Variables
_Expr_, like most programming languages, supports variables. A variable is an identifier with an assigned value. Variables are stored in _contexts_. _Expr_, like most programming languages, supports variables. A variable is an identifier with an assigned value. Variables are stored in _contexts_.
@@ -454,17 +548,23 @@ NOTE: The assign operator [blue]`=` returns the value assigned to the variable.
.Examples .Examples
`>>>` [blue]`a=1` + `>>>` [blue]`a=1` +
[green]`1` + [green]`1`
`>>>` [blue]`a_b=1+2` + `>>>` [blue]`a_b=1+2` +
[green]`1+2` + [green]`1+2`
`>>>` [blue]`a_b` + `>>>` [blue]`a_b` +
[green]`3` + [green]`3`
`>>>` [blue]`x = 5.2 * (9-3)` [gray]_// The assigned value has the approximation error typical of the float data-type_ +
[green]`31.200000000000003` + `>>>` [blue]`x = 5.2 * (9-3)` [gray]_// The assigned value has the typical approximation error of the float data-type_ +
[green]`31.200000000000003`
`>>>` [blue]`x = 1; y = 2*x` + `>>>` [blue]`x = 1; y = 2*x` +
[green]`2` + [green]`2`
`>>>` [blue]`_a=2` + `>>>` [blue]`_a=2` +
[red]`Parse Error: [1:2] unexpected token "_"` + [red]`Parse Error: [1:2] unexpected token "_"`
`>>>` [blue]`1=2` + `>>>` [blue]`1=2` +
[red]`Parse Error: assign operator ("=") must be preceded by a variable` [red]`Parse Error: assign operator ("=") must be preceded by a variable`
@@ -474,6 +574,11 @@ NOTE: The assign operator [blue]`=` returns the value assigned to the variable.
=== [blue]`;` operator === [blue]`;` operator
The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The value of the latter is the final result. The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The value of the latter is the final result.
.Mult-expression syntax
====
*_multi-expression_* = _expression_ {"**;**" _expression_ }
====
An expression that contains [blue]`;` is called a _multi-expression_ and each component expressione is called a _sub-expression_. An expression that contains [blue]`;` is called a _multi-expression_ and each component expressione is called a _sub-expression_.
IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions. IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions.
@@ -532,38 +637,47 @@ The [blue]`:` symbol (colon) is the separator of the selector-cases. Note that i
.Examples .Examples
`>>>` [blue]`1 ? {"a"} : {"b"}` + `>>>` [blue]`1 ? {"a"} : {"b"}` +
[green]`b` + [green]`b`
`>>>` [blue]`10 ? {"a"} : {"b"} :: {"c"}` +
[green]`c' + `>>>` [blue]`10 ? {"a"} : {"b"} {2c} {"c"}` +
[green]`>>>` [blue]`10 ? {"a"} :[true, 2+8] {"b"} :: {"c"}` + [green]`c`
[green]`b` +
`>>>` [blue]`10 ? {"a"} :[true, 2+8] {"b"} ::[10] {"c"}` + `>>>` [blue]`10 ? {"a"} :[true, 2+8] {"b"} {2c} {"c"}` +
[red]`Parse Error: [1:34] case list in default clause` + [green]`b`
[green]`>>>` [blue]`10 ? {"a"} :[10] {x="b" but x} :: {"c"}` +
[green]`b` + `>>>` [blue]`10 ? {"a"} :[true, 2+8] {"b"} {2c} [10] {"c"}` +
`>>>` [blue]`10 ? {"a"} :[10] {x="b"; x} :: {"c"}` + [red]`Parse Error: [1:34] case list in default clause`
[green]`b` +
`>>>` [blue]`10 ? {"a"} :[10] {x="b" but x} {2c} {"c"}` +
[green]`b`
`>>>` [blue]`10 ? {"a"} :[10] {x="b"; x} {2c} {"c"}` +
[green]`b`
`>>>` [blue]`10 ? {"a"} : {"b"}` + `>>>` [blue]`10 ? {"a"} : {"b"}` +
[red]`Eval Error: [1:3] no case catches the value (10) of the selection expression` [red]`Eval Error: [1:3] no case catches the value (10) of the selection expression`
=== Variable default value [blue]`??` and [blue]`?=` === Variable default value [blue]`??` and [blue]`?=`
The left operand of these two operators must be a variable. The right operator can be any expression. They return the value of the variable if this is define; otherwise they return the value of the right expression. The left operand of these two operators must be a variable. The right operator can be any expression. They return the value of the variable if this is defined; otherwise they return the value of the right expression.
IMPORTANT: If the left variable is defined, the right expression is not evuated at all. IMPORTANT: If the left variable is defined, the right expression is not evaluated at all.
The [blue]`??` do not change the status of the left variable. The [blue]`??` operator do not change the status of the left variable.
The [blue]`?=` assigns the calculated value of the right expression to the left variable. The [blue]`?=` assigns the calculated value of the right expression to the left variable.
.Examples .Examples
`>>>` [blue]`var ?? (1+2)`' `>>>` [blue]`var ?? (1+2)`' +
[green]`3` + [green]`3`
`>>>` [blue]`var` + `>>>` [blue]`var` +
[red]`Eval Error: undefined variable or function "var"` + [red]`Eval Error: undefined variable or function "var"`
`>>>` [blue]`var ?= (1+2)` + `>>>` [blue]`var ?= (1+2)` +
[green]`3` + [green]`3`
`>>>` [blue]`var` +
`>>>` [blue]`var`
[green]`3` [green]`3`
NOTE: These operators have a high priority, in particular higher than the operator [blue]`=`. NOTE: These operators have a high priority, in particular higher than the operator [blue]`=`.
@@ -576,57 +690,87 @@ The table below shows all supported operators by decreasing priorities.
|=== |===
| Priority | Operators | Position | Operation | Operands and results | Priority | Operators | Position | Operation | Operands and results
.2+|*ITEM*| [blue]`.` | _Infix_ | _List item_| _list_ `"."` _integer_ -> _any_ .2+|*ITEM*| [blue]`[`...`]` | _Postfix_ | _List item_| _list_ `[` _integer_ `]` -> _any_
| [blue]`.` | _Infix_ | _Dict item_ | _dict_ `"."` _any_ -> _any_ | [blue]`[`...`]` | _Postfix_ | _Dict item_ | _dict_ `[` _any_ `]` -> _any_
.2+|*INC*| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `"++"` -> _integer_ .2+|*INC*| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `++` -> _integer_
| [blue]`++` | _Postfix_ | _Next item_ | _iterator_ `"++"` -> _any_ | [blue]`++` | _Postfix_ | _Next item_ | _iterator_ `++` -> _any_
.1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ `"!"` -> _integer_ .2+|*DEFAULT*| [blue]`??` | _Infix_ | _Default value_| _variable_ `??` _any-expr_ -> _any_
.3+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| (`"+"`\|`"-"`) _number_ -> _number_ | [blue]`?=` | _Infix_ | _Default/assign value_| _variable_ `?=` _any-expr_ -> _any_
| [blue]`#` | _Prefix_ | _Lenght-of_ | `"#"` _collection_ -> _integer_ .1+| *ITER*^1^| [blue]`()` | _Prefix_ | _Iterator value_ | `()` _iterator_ -> _any_
| [blue]`#` | _Prefix_ | _Size-of_ | `"#"` _iterator_ -> _integer_ .1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ `!` -> _integer_
.5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ `"*"` _number_ -> _number_ .3+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| (`+`\|`-`) _number_ -> _number_
| [blue]`*` | _Infix_ | _String-repeat_ | _string_ `"*"` _integer_ -> _string_ | [blue]`#` | _Prefix_ | _Lenght-of_ | `#` _collection_ -> _integer_
| [blue]`/` | _Infix_ | _Division_ | _number_ `"/"` _number_ -> _number_ | [blue]`#` | _Prefix_ | _Size-of_ | `#` _iterator_ -> _integer_
| [blue]`./` | _Infix_ | _Float-division_ | __number__ `"./"` _number_ -> _float_ .2+|*SELECT*| [blue]`? : ::` | _Multi-Infix_ | _Case-Selector_ | _any-expr_ `?` _case-list_ _case-expr_ `:` _case-list_ _case-expr_ ... `::` _default-expr_ -> _any_
| [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ `"%"` _integer_ -> _integer_ | [blue]`? : ::` | _Multi-Infix_ | _Index-Selector_ | _int-expr_ `?` _case-expr_ `:` _case-expr_ ... `::` _default-expr_ -> _any_
.6+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ `"+"` _number_ -> _number_ .1+|*FRACT*| [blue]`\|` | _Infix_ | _Fraction_ | _integer_ `\|` _integer_ -> _fraction_
| [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) `"+"` (_string_\|_number_) -> _string_ .5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ `*` _number_ -> _number_
| [blue]`+` | _Infix_ | _List-join_ | _list_ `"+"` _list_ -> _list_ | [blue]`*` | _Infix_ | _String-repeat_ | _string_ `*` _integer_ -> _string_
| [blue]`+` | _Infix_ | _Dict-join_ | _dict_ `"+"` _dict_ -> _dict_ | [blue]`/` | _Infix_ | _Division_ | _number_ `/` _number_ -> _number_
| [blue]`-` | _Infix_ | _Subtraction_ | _number_ `"-"` _number_ -> _number_ | [blue]`./` | _Infix_ | _Float-division_ | __number__ `./` _number_ -> _float_
| [blue]`-` | _Infix_ | _List-difference_ | _list_ `"-"` _list_ -> _list_ | [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ `%` _integer_ -> _integer_
.8+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ `"<"` _comparable_ -> _boolean_ .6+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ `+` _number_ -> _number_
| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ `"\<="` _comparable_ -> _boolean_ | [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) `+` (_string_\|_number_) -> _string_
| [blue]`>` | _Infix_ | _greater_ | _comparable_ `">"` _comparable_ -> _boolean_ | [blue]`+` | _Infix_ | _List-join_ | _list_ `+` _list_ -> _list_
| [blue]`>=` | _Infix_ | _greater-equal_ | _comparable_ `">="` _comparable_ -> _boolean_ | [blue]`+` | _Infix_ | _Dict-join_ | _dict_ `+` _dict_ -> _dict_
| [blue]`==` | _Infix_ | _equal_ | _comparable_ `"=="` _comparable_ -> _boolean_ | [blue]`-` | _Infix_ | _Subtraction_ | _number_ `-` _number_ -> _number_
| [blue]`!=` | _Infix_ | _not-equal_ | _comparable_ `"!="` _comparable_ -> _boolean_ | [blue]`-` | _Infix_ | _List-difference_ | _list_ `-` _list_ -> _list_
| [blue]`in` | _Infix_ | _member-of-list_ | _any_ `"in"` _list_ -> _boolean_ .8+|*RELATION*| [blue]`<` | _Infix_ | _Less_ | _comparable_ `<` _comparable_ -> _boolean_
| [blue]`in` | _Infix_ | _key-of-dict_ | _any_ `"in"` _dict_ -> _boolean_ | [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ `\<=` _comparable_ -> _boolean_
.1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | `"not"` _boolean_ -> _boolean_ | [blue]`>` | _Infix_ | _Greater_ | _comparable_ `>` _comparable_ -> _boolean_
.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ `"and"` _boolean_ -> _boolean_ | [blue]`>=` | _Infix_ | _Greater-equal_ | _comparable_ `>=` _comparable_ -> _boolean_
| [blue]`&&` | _Infix_ | _and_ | _boolean_ `"&&"` _boolean_ -> _boolean_ | [blue]`==` | _Infix_ | _Equal_ | _comparable_ `==` _comparable_ -> _boolean_
.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ `"or"` _boolean_ -> _boolean_ | [blue]`!=` | _Infix_ | _Not-equal_ | _comparable_ `!=` _comparable_ -> _boolean_
| [blue]`\|\|` | _Infix_ | _or_ | _boolean_ `"\|\|"` _boolean_ -> _boolean_ | [blue]`in` | _Infix_ | _Member-of-list_ | _any_ `in` _list_ -> _boolean_
.3+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_ | [blue]`in` | _Infix_ | _Key-of-dict_ | _any_ `in` _dict_ -> _boolean_
| [blue]`>>` | _Infix_ | _front-insert_ | _any_ ">>" _list_ -> _list_ .1+|*NOT*| [blue]`not` | _Prefix_ | _Not_ | `not` _boolean_ -> _boolean_
| [blue]`<<` | _Infix_ | _back-insert_ | _list_ "<<" _any_ -> _list_ .2+|*AND*| [blue]`and` | _Infix_ | _And_ | _boolean_ `and` _boolean_ -> _boolean_
.1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_ | [blue]`&&` | _Infix_ | _And_ | _boolean_ `&&` _boolean_ -> _boolean_
.2+|*OR*| [blue]`or` | _Infix_ | _Or_ | _boolean_ `or` _boolean_ -> _boolean_
| [blue]`\|\|` | _Infix_ | _Or_ | _boolean_ `\|\|` _boolean_ -> _boolean_
.3+|*ASSIGN*| [blue]`=` | _Infix_ | _Assignment_ | _identifier_ `=` _any_ -> _any_
| [blue]`>>` | _Infix_ | _Front-insert_ | _any_ `>>` _list_ -> _list_
| [blue]`<<` | _Infix_ | _Back-insert_ | _list_ `<<` _any_ -> _list_
.1+|*BUT*| [blue]`but` | _Infix_ | _But_ | _any_ `but` _any_ -> _any_
.1+|*RANGE*| [blue]`:` | _Infix_ | _Index-range_ | _integer_ `:` _integer_ -> _integer-pair_
|=== |===
^1^ Experimental
== Functions == Functions
Functions in _Expr_ are very similar to functions available in many programming languages. Actually, _Expr_ supports two types of function, _expr-functions_ and _go-functions_. Functions in _Expr_ are very similar to functions available in many programming languages. Actually, _Expr_ supports two types of function, _expr-functions_ and _go-functions_.
* _expr-functions_ are defined using _Expr_'s syntax. They can be passed as arguments to other functions and can be returned from functions. Moreover, they bind themselves to the defining context, thus becoming closures. * _expr-functions_ are defined using _Expr_'s syntax. They can be passed as arguments to other functions and can be returned from functions. Moreover, they bind themselves to the defining context, thus becoming closures.
* _go-functions_ are regular Golang functions callable from _Expr_ expressions. They are defined in Golang source files called _modules_ and compiled within the _Expr_ package. To make Golang functions available in _Expr_ contextes, it is required to _import_ the module in which they are defined. * _go-functions_ are regular Golang functions callable from _Expr_ expressions. They are defined in Golang source files called _modules_ and compiled within the _Expr_ package. To make Golang functions available in _Expr_ contextes, it is required to _import_ the module in which they are defined.
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.
=== _Expr_ function definition
A function is identified and referenced by its name. It can have zero or more parameter. _Expr_ functions also support optional parameters.
. Expr's function definition syntax
====
*_function-definition_* = _identifier_ "**=**" "**func(**" [_param-list_] "**)**" "**{**" _multi-expression_ "**}**"
_param_list_ = _required-param-list_ [ "**,**" _optional-param-list_ ]
_required-param-list_ = _identifier_ { "**,**" _identifier_ }
_optional-param-list_ = _optional-parm_ { "**,**" _optional-param_ }
_optional-param_ = _identifier_ "**=**" _any-expr_
====
.Examples
#TODO#
=== _Golang_ function definition
Description of how to define Golan functions and how to bind them to _Expr_ are topics treated in another document that I'll write, one day, maybe.
=== Function calls === Function calls
#TODO: function calls operations# #TODO: function calls operations#
=== Function definitions 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.
#TODO: function definitions operations#
== Iterators
#TODO: function calls operations#
== Builtins == Builtins
#TODO: builtins# #TODO: builtins#
@@ -637,4 +781,7 @@ In _Expr_ functions compute values in a local context (scope) that do not make e
[blue]_import([grey]#<source-file>#)_ loads the multi-expression contained in the specified source and returns its value. [blue]_import([grey]#<source-file>#)_ loads the multi-expression contained in the specified source and returns its value.
== Plugins
#TODO: plugins#
+528 -212
View File
File diff suppressed because one or more lines are too long
+39
View File
@@ -199,3 +199,42 @@ func (info *funcInfo) MaxArgs() int {
func (info *funcInfo) Functor() Functor { func (info *funcInfo) Functor() Functor {
return info.functor return info.functor
} }
// ----- Call a function ---
func checkFunctionCall(ctx ExprContext, name string, varParams *[]any) (err error) {
if info, exists, owner := GetFuncInfo(ctx, name); exists {
passedCount := len(*varParams)
if info.MinArgs() > passedCount {
err = ErrTooFewParams(name, info.MinArgs(), info.MaxArgs(), passedCount)
}
for i, p := range info.Params() {
if i >= passedCount {
if !p.IsDefault() {
break
}
*varParams = append(*varParams, p.DefaultValue())
}
}
if err == nil && info.MaxArgs() >= 0 && info.MaxArgs() < len(*varParams) {
err = ErrTooMuchParams(name, info.MaxArgs(), len(*varParams))
}
if err == nil && owner != ctx {
ctx.RegisterFuncInfo(info)
}
} else {
err = fmt.Errorf("unknown function %s()", name)
}
return
}
func CallFunction(parentCtx ExprContext, name string, params []any) (result any, err error) {
ctx := cloneContext(parentCtx)
ctx.SetParent(parentCtx)
if err = checkFunctionCall(ctx, name, &params); err == nil {
result, err = ctx.Call(name, params)
exportObjectsToParent(ctx)
}
return
}
+2 -35
View File
@@ -6,7 +6,6 @@ package expr
import ( import (
"errors" "errors"
"fmt"
) )
// -------- function call term // -------- function call term
@@ -22,35 +21,7 @@ func newFuncCallTerm(tk *Token, args []*term) *term {
} }
// -------- eval func call // -------- eval func call
func checkFunctionCall(ctx ExprContext, name string, varParams *[]any) (err error) { func evalFuncCall(ctx ExprContext, self *term) (v any, err error) {
if info, exists, owner := GetFuncInfo(ctx, name); exists {
passedCount := len(*varParams)
if info.MinArgs() > passedCount {
err = ErrTooFewParams(name, info.MinArgs(), info.MaxArgs(), passedCount)
}
for i, p := range info.Params() {
if i >= passedCount {
if !p.IsDefault() {
break
}
*varParams = append(*varParams, p.DefaultValue())
}
}
if err == nil && info.MaxArgs() >= 0 && info.MaxArgs() < len(*varParams) {
err = ErrTooMuchParams(name, info.MaxArgs(), len(*varParams))
}
if err == nil && owner != ctx {
ctx.RegisterFuncInfo(info)
}
} else {
err = fmt.Errorf("unknown function %s()", name)
}
return
}
func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) {
ctx := cloneContext(parentCtx)
ctx.SetParent(parentCtx)
name, _ := self.tk.Value.(string) name, _ := self.tk.Value.(string)
params := make([]any, len(self.children), len(self.children)+5) params := make([]any, len(self.children), len(self.children)+5)
for i, tree := range self.children { for i, tree := range self.children {
@@ -62,11 +33,7 @@ func evalFuncCall(parentCtx ExprContext, self *term) (v any, err error) {
} }
if err == nil { if err == nil {
if err = checkFunctionCall(ctx, name, &params); err == nil { v, err = CallFunction(ctx, name, params)
if v, err = ctx.Call(name, params); err == nil {
exportObjects(parentCtx, ctx)
}
}
} }
return return
} }
+30 -3
View File
@@ -40,11 +40,14 @@ func verifyRange(indexTerm *term, indexList *ListType, maxValue int) (startIndex
v, _ := ((*indexList)[0]).(*intPair) v, _ := ((*indexList)[0]).(*intPair)
startIndex = v.a startIndex = v.a
endIndex = v.b endIndex = v.b
if endIndex == ConstLastIndex {
endIndex = maxValue
}
if startIndex < 0 && startIndex >= -maxValue { if startIndex < 0 && startIndex >= -maxValue {
startIndex = maxValue + startIndex startIndex = maxValue + startIndex
} }
if endIndex < 0 && endIndex >= -maxValue { if endIndex < 0 && endIndex >= -maxValue {
endIndex = maxValue + endIndex + 1 endIndex = maxValue + endIndex
} }
if startIndex < 0 || startIndex > maxValue { if startIndex < 0 || startIndex > maxValue {
err = indexTerm.Errorf("range start-index %d is out of bounds", startIndex) err = indexTerm.Errorf("range start-index %d is out of bounds", startIndex)
@@ -87,13 +90,14 @@ func evalIndex(ctx ExprContext, self *term) (v any, err error) {
v = string(unboxedValue[index]) v = string(unboxedValue[index])
} }
case *DictType: case *DictType:
var ok bool /* var ok bool
var indexValue any var indexValue any
if indexValue, err = verifyKey(indexTerm, indexList); err == nil { if indexValue, err = verifyKey(indexTerm, indexList); err == nil {
if v, ok = (*unboxedValue)[indexValue]; !ok { if v, ok = (*unboxedValue)[indexValue]; !ok {
err = indexTerm.Errorf("key %v does not belong to the dictionary", rightValue) err = indexTerm.Errorf("key %v does not belong to the dictionary", rightValue)
} }
} } */
v, err = getDictItem(unboxedValue, indexTerm, indexList, rightValue)
default: default:
err = self.errIncompatibleTypes(leftValue, rightValue) err = self.errIncompatibleTypes(leftValue, rightValue)
} }
@@ -113,6 +117,29 @@ func evalIndex(ctx ExprContext, self *term) (v any, err error) {
default: default:
err = self.errIncompatibleTypes(leftValue, rightValue) err = self.errIncompatibleTypes(leftValue, rightValue)
} }
} else if IsDict(leftValue) {
d := leftValue.(*DictType)
/* var ok bool
var indexValue any
if indexValue, err = verifyKey(indexTerm, indexList); err == nil {
if v, ok = (*d)[indexValue]; !ok {
err = indexTerm.Errorf("key %v does not belong to the dictionary", rightValue)
}
}*/
v, err = getDictItem(d, indexTerm, indexList, rightValue)
}
return
}
func getDictItem(d *DictType, indexTerm *term, indexList *ListType, rightValue any) (v any, err error) {
var ok bool
var indexValue any
if indexValue, err = verifyKey(indexTerm, indexList); err == nil {
if v, ok = (*d)[indexValue]; !ok {
err = indexTerm.Errorf("key %v does not belong to the dictionary", rightValue)
}
} }
return return
} }
+2 -2
View File
@@ -29,7 +29,7 @@ func newRangeTerm(tk *Token) (inst *term) {
tk: *tk, tk: *tk,
children: make([]*term, 0, 2), children: make([]*term, 0, 2),
position: posInfix, position: posInfix,
priority: priDot, priority: priRange,
evalFunc: evalRange, evalFunc: evalRange,
} }
} }
@@ -47,7 +47,7 @@ func evalRange(ctx ExprContext, self *term) (v any, err error) {
if leftValue, err = self.children[0].compute(ctx); err != nil { if leftValue, err = self.children[0].compute(ctx); err != nil {
return return
} }
rightValue = int64(-1) rightValue = int64(ConstLastIndex)
} else if leftValue, rightValue, err = self.evalInfix(ctx); err != nil { } else if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
return return
} }
+13 -1
View File
@@ -325,6 +325,14 @@ func couldBeACollection(t *term) bool {
return sym == SymList || sym == SymString || sym == SymDict || sym == SymExpression || sym == SymVariable return sym == SymList || sym == SymString || sym == SymDict || sym == SymExpression || sym == SymVariable
} }
// func areSymbolsOutOfCtx(tk *Token, ctxTerm *term, syms ...Symbol) bool {
// var areOut = false
// if ctxTerm != nil {
// areOut = tk.IsOneOf(syms)
// }
// return areOut
// }
func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef bool, termSymbols ...Symbol) (tree *ast, err error) { func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef bool, termSymbols ...Symbol) (tree *ast, err error) {
var selectorTerm *term = nil var selectorTerm *term = nil
var currentTerm *term = nil var currentTerm *term = nil
@@ -332,7 +340,7 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef
tree = NewAst() tree = NewAst()
firstToken := true firstToken := true
// lastSym := SymUnknown // lastSym := SymUnknown
for tk = scanner.Next(); err == nil && tk != nil && !tk.IsTerm(termSymbols); tk = scanner.Next() { for tk = scanner.Next(); err == nil && tk != nil && !tk.IsTerm(termSymbols); /*&& !areSymbolsOutOfCtx(tk, selectorTerm, SymColon, SymDoubleColon)*/ tk = scanner.Next() {
if tk.Sym == SymComment { if tk.Sym == SymComment {
continue continue
} }
@@ -438,6 +446,10 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef
} else { } else {
currentTerm, err = tree.addToken2(tk) currentTerm, err = tree.addToken2(tk)
} }
if tk.IsSymbol(SymColon) {
// Colon outside a selector term acts like a separator
firstToken = true
}
default: default:
currentTerm, err = tree.addToken2(tk) currentTerm, err = tree.addToken2(tk)
} }
+14 -1
View File
@@ -6,7 +6,9 @@ package expr
import ( import (
"fmt" "fmt"
"os"
"plugin" "plugin"
"strings"
) )
var pluginRegister map[string]*plugin.Plugin var pluginRegister map[string]*plugin.Plugin
@@ -25,6 +27,17 @@ func pluginExists(name string) (exists bool) {
return return
} }
func makePluginName(name string) (decorated string) {
var template string
if execName, err := os.Executable(); err != nil || strings.Index(execName, "debug") < 0 {
template = "expr-%s-plugin.so"
} else {
template = "expr-%s-plugin.so.debug"
}
decorated = fmt.Sprintf(template, name)
return
}
func importPlugin( /*ctx ExprContext,*/ dirList []string, name string) (err error) { func importPlugin( /*ctx ExprContext,*/ dirList []string, name string) (err error) {
var filePath string var filePath string
var p *plugin.Plugin var p *plugin.Plugin
@@ -33,7 +46,7 @@ func importPlugin( /*ctx ExprContext,*/ dirList []string, name string) (err erro
var importFunc func(ExprContext) var importFunc func(ExprContext)
var ok bool var ok bool
decoratedName := fmt.Sprintf("expr-%s-plugin.so", name) decoratedName := makePluginName(name)
if filePath, err = makeFilepath(decoratedName, dirList); err != nil { if filePath, err = makeFilepath(decoratedName, dirList); err != nil {
return return
+7 -1
View File
@@ -7,7 +7,7 @@ package expr
import ( import (
"fmt" "fmt"
"slices" "slices"
// "strings" // "strings"
) )
type SimpleStore struct { type SimpleStore struct {
@@ -50,6 +50,7 @@ func (ctx *SimpleStore) Merge(src ExprContext) {
ctx.funcStore[name], _ = src.GetFuncInfo(name) ctx.funcStore[name], _ = src.GetFuncInfo(name)
} }
} }
/* /*
func varsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) { func varsCtxToBuilder(sb *strings.Builder, ctx ExprContext, indent int) {
sb.WriteString("vars: {\n") sb.WriteString("vars: {\n")
@@ -165,6 +166,11 @@ func (ctx *SimpleStore) GetVar(varName string) (v any, exists bool) {
return return
} }
func (ctx *SimpleStore) GetLast() (v any) {
v = ctx.varStore["last"]
return
}
func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) { func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) {
// fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value) // fmt.Printf("[%p] setVar(%v, %v)\n", ctx, varName, value)
ctx.varStore[varName] = value ctx.varStore[varName] = value
+4 -4
View File
@@ -16,10 +16,10 @@ func TestFuncOs(t *testing.T) {
/* 2 */ {`builtin "os.file"; handle=fileOpen("/etc/hosts"); fileClose(handle)`, true, nil}, /* 2 */ {`builtin "os.file"; handle=fileOpen("/etc/hosts"); fileClose(handle)`, true, nil},
/* 3 */ {`builtin "os.file"; handle=fileOpen("/etc/hostsX")`, nil, errors.New(`open /etc/hostsX: no such file or directory`)}, /* 3 */ {`builtin "os.file"; handle=fileOpen("/etc/hostsX")`, nil, errors.New(`open /etc/hostsX: no such file or directory`)},
/* 4 */ {`builtin "os.file"; handle=fileCreate("/tmp/dummy"); fileClose(handle)`, true, nil}, /* 4 */ {`builtin "os.file"; handle=fileCreate("/tmp/dummy"); fileClose(handle)`, true, nil},
/* 5 */ {`builtin "os.file"; handle=fileAppend("/tmp/dummy"); fileWrite(handle, "bye-bye"); fileClose(handle)`, true, nil}, /* 5 */ {`builtin "os.file"; handle=fileAppend("/tmp/dummy"); fileWriteText(handle, "bye-bye"); fileClose(handle)`, true, nil},
/* 6 */ {`builtin "os.file"; handle=fileOpen("/tmp/dummy"); word=fileRead(handle, "-"); fileClose(handle);word`, "bye", nil}, /* 6 */ {`builtin "os.file"; handle=fileOpen("/tmp/dummy"); word=fileReadText(handle, "-"); fileClose(handle);word`, "bye", nil},
/* 7 */ {`builtin "os.file"; word=fileRead(nil, "-")`, nil, errors.New(`readFileFunc(): invalid file handle`)}, /* 7 */ {`builtin "os.file"; word=fileReadText(nil, "-")`, nil, errors.New(`fileReadText(): invalid file handle`)},
/* 7 */ {`builtin "os.file"; fileWrite(nil, "bye")`, nil, errors.New(`writeFileFunc(): invalid file handle`)}, /* 7 */ {`builtin "os.file"; fileWriteText(nil, "bye")`, nil, errors.New(`fileWriteText(): invalid file handle`)},
} }
// t.Setenv("EXPR_PATH", ".") // t.Setenv("EXPR_PATH", ".")
+1
View File
@@ -31,6 +31,7 @@ func TestDictParser(t *testing.T) {
/* 8 */ {`D={"a":1, "b":2}; D["a"]=9; D`, map[any]any{"a": 9, "b": 2}, nil}, /* 8 */ {`D={"a":1, "b":2}; D["a"]=9; D`, map[any]any{"a": 9, "b": 2}, nil},
/* 9 */ {`D={"a":1, "b":2}; D["z"]=9; D`, map[any]any{"z": 9, "a": 1, "b": 2}, nil}, /* 9 */ {`D={"a":1, "b":2}; D["z"]=9; D`, map[any]any{"z": 9, "a": 1, "b": 2}, nil},
/* 10 */ {`D={"a":1, "b":2}; D[nil]=9`, nil, errors.New(`[1:21] index/key is nil`)}, /* 10 */ {`D={"a":1, "b":2}; D[nil]=9`, nil, errors.New(`[1:21] index/key is nil`)},
/* 11 */ {`D={"a":1, "b":2}; D["a"]`, int64(1), nil},
} }
succeeded := 0 succeeded := 0
+1 -1
View File
@@ -14,7 +14,7 @@ func TestExpr(t *testing.T) {
inputs := []inputType{ inputs := []inputType{
/* 1 */ {`0?{}`, nil, nil}, /* 1 */ {`0?{}`, nil, nil},
/* 2 */ {`fact=func(n){(n)?{1}::{n*fact(n-1)}}; fact(5)`, int64(120), nil}, /* 2 */ {`fact=func(n){(n)?{1}::{n*fact(n-1)}}; fact(5)`, int64(120), nil},
/* 3 */ {`builtin "os.file"; f=fileOpen("test-file.txt"); line=fileRead(f); fileClose(f); line`, "uno", nil}, /* 3 */ {`builtin "os.file"; f=fileOpen("test-file.txt"); line=fileReadText(f); fileClose(f); line`, "uno", nil},
/* 4 */ {`mynot=func(v){int(v)?{true}::{false}}; mynot(0)`, true, nil}, /* 4 */ {`mynot=func(v){int(v)?{true}::{false}}; mynot(0)`, true, nil},
/* 5 */ {`1 ? {1} : [1+0] {3*(1+1)}`, int64(6), nil}, /* 5 */ {`1 ? {1} : [1+0] {3*(1+1)}`, int64(6), nil},
/* 6 */ {` /* 6 */ {`
+3 -3
View File
@@ -14,9 +14,9 @@ func TestIteratorParser(t *testing.T) {
/* 4 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; it.reset; ()it`, int64(0), nil}, /* 4 */ {`include "test-resources/iterator.expr"; it=$(ds,3); it++; it++; it.reset; ()it`, int64(0), nil},
/* 5 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); add(it)`, int64(6), nil}, /* 5 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); add(it)`, int64(6), nil},
/* 6 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil}, /* 6 */ {`builtin "math.arith"; include "test-resources/iterator.expr"; it=$(ds,3); mul(it)`, int64(0), nil},
/* 7 */ {`builtin "math.arith"; include "test-resources/file-reader.expr"; it=$(ds,"int.list"); mul(it)`, int64(12000), nil}, /* 7 */ {`builtin "math.arith"; include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); mul(it)`, int64(12000), nil},
/* 8 */ {`include "test-resources/file-reader.expr"; it=$(ds,"int.list"); it++; it.index`, int64(0), nil}, /* 8 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it++; it.index`, int64(0), nil},
/* 10 */ {`include "test-resources/file-reader.expr"; it=$(ds,"int.list"); it.clean`, true, nil}, /* 10 */ {`include "test-resources/file-reader.expr"; it=$(ds,"test-resources/int.list"); it.clean`, true, nil},
/* 11 */ {`it=$(1,2,3); it++`, int64(1), nil}, /* 11 */ {`it=$(1,2,3); it++`, int64(1), nil},
} }
// inputs1 := []inputType{ // inputs1 := []inputType{
+4 -2
View File
@@ -49,8 +49,10 @@ func TestListParser(t *testing.T) {
/* 35 */ {`L=[1,2]; L[5]=9; L`, nil, errors.New(`index 5 out of bounds (0, 1)`)}, /* 35 */ {`L=[1,2]; L[5]=9; L`, nil, errors.New(`index 5 out of bounds (0, 1)`)},
/* 36 */ {`L=[1,2]; L[]=9; L`, nil, errors.New(`[1:12] index/key specification expected, got [] [list]`)}, /* 36 */ {`L=[1,2]; L[]=9; L`, nil, errors.New(`[1:12] index/key specification expected, got [] [list]`)},
/* 37 */ {`L=[1,2]; L[nil]=9;`, nil, errors.New(`[1:12] index/key is nil`)}, /* 37 */ {`L=[1,2]; L[nil]=9;`, nil, errors.New(`[1:12] index/key is nil`)},
// /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil}, /* 38 */ {`[0,1,2,3,4][2:3]`, newListA(int64(2)), nil},
// /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil}, /* 39 */ {`[0,1,2,3,4][3:-1]`, newListA(int64(3)), nil},
/* 40 */ {`[0,1,2,3,4][-3:-1]`, newListA(int64(2), int64(3)), nil},
/* 41 */ {`[0,1,2,3,4][0:]`, newListA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
} }
// t.Setenv("EXPR_PATH", ".") // t.Setenv("EXPR_PATH", ".")
+10 -11
View File
@@ -1,23 +1,22 @@
builtin ["os.file", "base"]; builtin ["os.file", "base"];
readInt=func(fh){ readInt=func(fh){
line=fileRead(fh); line=fileReadText(fh);
line ? [nil] {nil} :: {int(line)} line ? [nil] {nil} :: {int(line)}
}; };
ds={ ds={
"init":func(filename){ "init":func(filename){
fh=fileOpen(filename); fh=fileOpen(filename);
fh ? [nil] {nil} :: { @current=readInt(fh); @prev=@current }; fh ? [nil] {nil} :: { @current=readInt(fh) };
fh fh
}, },
"current":func(){ "current":func(){
prev current
}, },
"next":func(fh){ "next":func(fh){
current ? @current=readInt(fh);
[nil] {current} current
:: {@prev=current; @current=readInt(fh) but current}
}, },
"clean":func(fh){ "clean":func(fh){
fileClose(fh) fileClose(fh)
@@ -25,9 +24,9 @@ ds={
} }
//;f=$(ds, "int.list") //;f=$(ds, "int.list")
/*
;f++ //;f++
;f++ //;f++
;f++ //;f++
*/ //*/
//;add(f) //;add(f)
+5 -1
View File
@@ -60,7 +60,11 @@ func (tk *Token) IsError() bool {
} }
func (tk *Token) IsTerm(termSymbols []Symbol) bool { func (tk *Token) IsTerm(termSymbols []Symbol) bool {
return tk.IsEos() || tk.IsError() || (termSymbols != nil && slices.Index(termSymbols, tk.Sym) >= 0) return tk.IsEos() || tk.IsError() || tk.IsOneOf(termSymbols)
}
func (tk *Token) IsOneOf(termSymbols []Symbol) bool {
return termSymbols != nil && slices.Index(termSymbols, tk.Sym) >= 0
} }
func (tk *Token) IsSymbol(sym Symbol) bool { func (tk *Token) IsSymbol(sym Symbol) bool {