Compare commits
4 Commits
760c1ee6da
...
5ecf81412e
Author | SHA1 | Date | |
---|---|---|---|
5ecf81412e | |||
ff4db34f7b | |||
0f848071c2 | |||
6b3351b324 |
@ -741,8 +741,11 @@ The table below shows all supported operators by decreasing priorities.
|
||||
|
||||
.2+|*ITEM*| [blue]`[`...`]` | _Postfix_ | _List item_| _list_ `[` _integer_ `]` -> _any_
|
||||
| [blue]`[`...`]` | _Postfix_ | _Dict item_ | _dict_ `[` _any_ `]` -> _any_
|
||||
.2+|*INC*| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `++` -> _integer_
|
||||
| [blue]`++` | _Postfix_ | _Next item_ | _iterator_ `++` -> _any_
|
||||
.5+|*INC/DEC*| [blue]`++` | _Postfix_ | _Iterator next item_ | _iterator_ `++` -> _any_
|
||||
| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `++` -> _integer_
|
||||
| [blue]`++` | _Prefix_ | _Pre increment_ | `++` _integer-variable_ -> _integer_
|
||||
| [blue]`--` | _Postfix_ | _Post decrement_ | _integer-variable_ `--` -> _integer_
|
||||
| [blue]`--` | _Prefix_ | _Pre decrement_ | `--` _integer-variable_ -> _integer_
|
||||
.3+|*DEFAULT*| [blue]`??` | _Infix_ | _Default value_| _variable_ `??` _any-expr_ -> _any_
|
||||
| [blue]`?=` | _Infix_ | _Default/assign value_| _variable_ `?=` _any-expr_ -> _any_
|
||||
| [blue]`?!` | _Infix_ | _Alternate value_| _variable_ `?!` _any-expr_ -> _any_
|
||||
@ -769,9 +772,10 @@ The table below shows all supported operators by decreasing priorities.
|
||||
| [blue]`+` | _Infix_ | _Dict-join_ | _dict_ `+` _dict_ -> _dict_
|
||||
| [blue]`-` | _Infix_ | _Subtraction_ | _number_ `-` _number_ -> _number_
|
||||
| [blue]`-` | _Infix_ | _List-difference_ | _list_ `-` _list_ -> _list_
|
||||
.1+|*BITWISE NOT*| [blue]`~` | _Prefix_ | _Binary Not_ | `~` _number_ -> _number_
|
||||
.1+|*BITWISE AND*| [blue]`&` | _Infix_ | _Binary And_ | _number_ `&` _number_ -> _number_
|
||||
.1+|*BITWISE OR*| [blue]`\|` | _Infix_ | _Binary Or_ | _number_ `\|` _number_ -> _number_
|
||||
.1+|*BITWISE NOT*| [blue]`~` | _Prefix_ | _Binary Not_ | `~` _integer_ -> _integer_
|
||||
.1+|*BITWISE AND*| [blue]`&` | _Infix_ | _Binary And_ | _integer_ `&` _integer_ -> _integer_
|
||||
.2+|*BITWISE OR*| [blue]`\|` | _Infix_ | _Binary Or_ | _integer_ `\|` _integer_ -> _integer_
|
||||
| [blue]`^` | _Infix_ | _Binary Xor_ | _integer_ `^` _integer_ -> _integer_
|
||||
.8+|*RELATION*| [blue]`<` | _Infix_ | _Less_ | _comparable_ `<` _comparable_ -> _boolean_
|
||||
| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ `\<=` _comparable_ -> _boolean_
|
||||
| [blue]`>` | _Infix_ | _Greater_ | _comparable_ `>` _comparable_ -> _boolean_
|
||||
@ -788,7 +792,7 @@ The table below shows all supported operators by decreasing priorities.
|
||||
.2+|*INSERT*| [blue]`+>` | _Infix_ | _Prepend_ | _any_ `+>` _list_ -> _list_
|
||||
| [blue]`<+` | _Infix_ | _Append_ | _list_ `<+` _any_ -> _list_
|
||||
.2+|*ASSIGN*| [blue]`=` | _Infix_ | _Assignment_ | _identifier_ `=` _any_ -> _any_
|
||||
4+| _See also the special assignment operators table below_
|
||||
4+| _See also the table of special allocation operators below_
|
||||
.1+|*BUT*| [blue]`but` | _Infix_ | _But_ | _any_ `but` _any_ -> _any_
|
||||
.1+|*RANGE*| [blue]`:` | _Infix_ | _Index-range_ | _integer_ `:` _integer_ -> _integer-pair_
|
||||
|===
|
||||
|
@ -1930,7 +1930,13 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>dict</em> <code>[</code> <em>any</em> <code>]</code> → <em>any</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top" rowspan="2"><p class="tableblock"><strong>INC</strong></p></td>
|
||||
<td class="tableblock halign-center valign-top" rowspan="5"><p class="tableblock"><strong>INC/DEC</strong></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">++</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Postfix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Iterator next item</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>iterator</em> <code>++</code> → <em>any</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">++</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Postfix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Post increment</em></p></td>
|
||||
@ -1938,9 +1944,21 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">++</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Prefix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Pre increment</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>++</code> <em>integer-variable</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">--</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Postfix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Next item</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>iterator</em> <code>++</code> → <em>any</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Post decrement</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>integer-variable</em> <code>--</code> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">--</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Prefix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Pre decrement</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>--</code> <em>integer-variable</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top" rowspan="3"><p class="tableblock"><strong>DEFAULT</strong></p></td>
|
||||
@ -2105,21 +2123,27 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">~</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Prefix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Binary Not</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~</code> <em>number</em> → <em>number</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code>~</code> <em>integer</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>BITWISE AND</strong></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">&</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Infix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Binary And</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>number</em> <code>&</code> <em>number</em> → <em>number</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>integer</em> <code>&</code> <em>integer</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>BITWISE OR</strong></p></td>
|
||||
<td class="tableblock halign-center valign-top" rowspan="2"><p class="tableblock"><strong>BITWISE OR</strong></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">|</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Infix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Binary Or</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>number</em> <code>|</code> <em>number</em> → <em>number</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>integer</em> <code>|</code> <em>integer</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">^</code></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Infix</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Binary Xor</em></p></td>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>integer</em> <code>^</code> <em>integer</em> → <em>integer</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top" rowspan="8"><p class="tableblock"><strong>RELATION</strong></p></td>
|
||||
@ -2224,7 +2248,7 @@ These operators have a high priority, in particular higher than the operator <co
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>identifier</em> <code>=</code> <em>any</em> → <em>any</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top" colspan="4"><p class="tableblock"><em>See also the special assignment operators table below</em></p></td>
|
||||
<td class="tableblock halign-center valign-top" colspan="4"><p class="tableblock"><em>See also the table of special allocation operators below</em></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-center valign-top"><p class="tableblock"><strong>BUT</strong></p></td>
|
||||
@ -2575,7 +2599,7 @@ g(@p):any{}`
|
||||
</div>
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Last updated 2025-01-03 05:42:18 +0100
|
||||
Last updated 2025-01-05 12:15:58 +0100
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-post-inc.go
|
||||
// operator-post-inc-dec.go
|
||||
package expr
|
||||
|
||||
// -------- post increment term
|
69
operator-pre-inc-dec.go
Normal file
69
operator-pre-inc-dec.go
Normal file
@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-pre-inc-dec.go
|
||||
package expr
|
||||
|
||||
// -------- pre increment term
|
||||
|
||||
func newPreIncTerm(tk *Token) *term {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
parent: nil,
|
||||
children: make([]*term, 0, 1),
|
||||
position: posPrefix,
|
||||
priority: priIncDec,
|
||||
evalFunc: evalPreInc,
|
||||
}
|
||||
}
|
||||
|
||||
func evalPreInc(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
var childValue any
|
||||
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if IsInteger(childValue) && opTerm.children[0].symbol() == SymVariable {
|
||||
i := childValue.(int64) + 1
|
||||
ctx.SetVar(opTerm.children[0].source(), i)
|
||||
v = i
|
||||
} else {
|
||||
err = opTerm.errIncompatibleType(childValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// -------- pre decrement term
|
||||
|
||||
func newPreDecTerm(tk *Token) *term {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
parent: nil,
|
||||
children: make([]*term, 0, 1),
|
||||
position: posPrefix,
|
||||
priority: priIncDec,
|
||||
evalFunc: evalPreDec,
|
||||
}
|
||||
}
|
||||
|
||||
func evalPreDec(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
var childValue any
|
||||
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if IsInteger(childValue) && opTerm.children[0].symbol() == SymVariable {
|
||||
i := childValue.(int64) - 1
|
||||
ctx.SetVar(opTerm.children[0].source(), i)
|
||||
v = i
|
||||
} else {
|
||||
err = opTerm.errIncompatibleType(childValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymPreInc, newPreIncTerm)
|
||||
registerTermConstructor(SymPreDec, newPreDecTerm)
|
||||
}
|
37
parser.go
37
parser.go
@ -409,6 +409,23 @@ func listSubTree(tree *ast, listTerm *term, allowIndeces bool) (root *term, err
|
||||
return
|
||||
}
|
||||
|
||||
func changePrefix(tk *Token) {
|
||||
switch tk.Sym {
|
||||
case SymMinus:
|
||||
tk.SetSymbol(SymChangeSign)
|
||||
case SymPlus:
|
||||
tk.SetSymbol(SymUnchangeSign)
|
||||
case SymStar:
|
||||
tk.SetSymbol(SymDereference)
|
||||
case SymExclamation:
|
||||
tk.SetSymbol(SymNot)
|
||||
case SymDoublePlus:
|
||||
tk.SetSymbol(SymPreInc)
|
||||
case SymDoubleMinus:
|
||||
tk.SetSymbol(SymPreDec)
|
||||
}
|
||||
}
|
||||
|
||||
func (parser *parser) parseGeneral(scanner *scanner, ctx parserContext, termSymbols ...Symbol) (tree *ast, err error) {
|
||||
var selectorTerm *term = nil
|
||||
var currentTerm *term = nil
|
||||
@ -437,16 +454,16 @@ func (parser *parser) parseGeneral(scanner *scanner, ctx parserContext, termSymb
|
||||
|
||||
//fmt.Println("Token:", tk)
|
||||
if firstToken {
|
||||
if tk.Sym == SymMinus {
|
||||
tk.Sym = SymChangeSign
|
||||
} else if tk.Sym == SymPlus {
|
||||
tk.Sym = SymUnchangeSign
|
||||
} else if tk.IsSymbol(SymStar) {
|
||||
tk.SetSymbol(SymDereference)
|
||||
} else if tk.IsSymbol(SymExclamation) {
|
||||
err = tk.Errorf("postfix opertor %q requires an operand on its left side", tk)
|
||||
break
|
||||
}
|
||||
changePrefix(tk)
|
||||
// if tk.Sym == SymMinus {
|
||||
// tk.Sym = SymChangeSign
|
||||
// } else if tk.Sym == SymPlus {
|
||||
// tk.Sym = SymUnchangeSign
|
||||
// } else if tk.IsSymbol(SymStar) {
|
||||
// tk.SetSymbol(SymDereference)
|
||||
// } else if tk.IsSymbol(SymExclamation) {
|
||||
// tk.SetSymbol(SymNot)
|
||||
// }
|
||||
firstToken = false
|
||||
}
|
||||
|
||||
|
@ -103,6 +103,8 @@ func init() {
|
||||
SymCaretEqual: {"^=", symClassOperator, posInfix}, // 66: '^='
|
||||
SymPlusGreater: {"+>", symClassOperator, posInfix}, // 67: '+>'
|
||||
SymLessPlus: {"<+", symClassOperator, posInfix}, // 68: '<+'
|
||||
SymPreInc: {"++", symClassOperator, posPrefix}, // : '++'
|
||||
SymPreDec: {"--", symClassOperator, posPrefix}, // : '--'
|
||||
// SymChangeSign
|
||||
// SymUnchangeSign
|
||||
// SymIdentifier
|
||||
|
@ -81,6 +81,8 @@ const (
|
||||
SymChangeSign
|
||||
SymUnchangeSign
|
||||
SymDereference
|
||||
SymPreInc
|
||||
SymPreDec
|
||||
SymIdentifier
|
||||
SymBool
|
||||
SymInteger
|
||||
|
@ -26,12 +26,13 @@ func TestBool(t *testing.T) {
|
||||
/* 12 */ {`true or false`, true, nil},
|
||||
/* 13 */ {`true or []`, true, nil},
|
||||
/* 14 */ {`[] or false`, nil, errors.New(`got list as left operand type of 'OR' operator, it must be bool`)},
|
||||
/* 15 */ {`!true`, false, nil},
|
||||
/* 13 */ //{`true or []`, nil, errors.New(`[1:8] left operand 'true' [bool] and right operand '[]' [list] are not compatible with operator "OR"`)},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 1)
|
||||
// runTestSuiteSpec(t, section, inputs, 15)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,8 @@ func TestOperator(t *testing.T) {
|
||||
/* 18 */ {`1^2`, int64(3), nil},
|
||||
/* 19 */ {`3^2`, int64(1), nil},
|
||||
/* 19 */ {`a=1; a^=2`, int64(3), nil},
|
||||
/* 20 */ {`a=1; ++a`, int64(2), nil},
|
||||
/* 21 */ {`a=1; --a`, int64(0), nil},
|
||||
}
|
||||
|
||||
// t.Setenv("EXPR_PATH", ".")
|
||||
|
Loading…
Reference in New Issue
Block a user