Comments (13)
Hmm, in my rough understanding, statement structure will be like bellow?
SwitchStatement of expr * (tvalue, ast) list * ast option
and if switch case is like
switch(expr){
case "foo":
case "bar": ast1
case 100: ast2
default: ast3
}
it becomes
SwitchStatement(
expr, [
(Tstr "foo", ast1); (* case label "foo" *)
(Tstr "bar", ast1); (* case label "bar" *)
(Tint 100, ast2); (* case label 100 *)
], Some ast3) (* default *)
then expr
is evaluated only once(unlike if else statement), and matched case is searched by
let value = value_of_expr env ctx expr in
if jg_eq_eq value (Tstr "foo") then ast1
else if jg_eq_eq value (Tint 100) then ast2
else match ast3 with Some ast3 -> ast3 | None -> []
Both expr value and label value of case are tvalue, then we can compare them by using jg_eq_eq
.
Is it the "pattern matching" you said?
from jingoo.
See geneanet@1d220ad for a first draft. It does not handle multiple case
evaluating to same value yet.
from jingoo.
Thanks for reference, I've read it, compact and beautiful implementation!
from jingoo.
With geneanet@c026c28
You can use {% case foo || bar || baz %}
. What do you think about this syntax?
from jingoo.
After reading code, I can understand the meaning easily, but in first look, foo || bar || baz
part seems like boolean value?
if ((foo || bar || baz) === true){ ... }
But it's my own feeling...
from jingoo.
I should have written
{% switch x %}
{% case "foo" || "bar" || "baz" %}{{ ... }}
{% default %} {{ ... }}
{% endswitch %}
from jingoo.
Are case values allowed for direct value only?
In current impl, it seems it can be variable.
SwitchStatement of expression * (expression list * ast) list
If only for direct value, this should be like this?
SwitchStatement of expression * (tvalue list * ast) list
from jingoo.
But if it's only allowed for direct values, {% case "foo" || "bar" || "baz" %}
seems short and good syntax.
from jingoo.
I am actually using it with variable in my code.
{%- set T_ROOT = 0 -%}
...
{%- set T_WITNESS = 7 -%}
{%- set T_GODPARENT = 8 -%}
...
{%- switch r -%}
{% case T_ROOT || T_WITNESS || T_GODPARENT %}{{ ... }}
{%- default -%}{{... }}
{%- endswitch -%}
Would you say that {% case "foo" case "bar" case "baz" %}
would be a better/good choice. Or something else?
from jingoo.
What I worry about is confliction like this,
{% set a = false %}
{% set b = true %}
{% set c = false %}
{# good! #}
{% case a || b || c %} {{ ast }} -> Case([a,b,c], {{ ast }})
{# oops! #}
{% case a || b || c %} {{ ast }} -> {% case (a || b || c) %}{{ast}} -> Case([b], {{ast}})
If this kind of reducing never happen, I'm ok, but I've not read all current parser yet, so I can't judge.
from jingoo.
What happens here is that it is parsed as:
{% case a || b || c %} {{ ast }} -> {% case (a || b || c) %}{{ast}}
But parsed output is processed (in the parser):
let rec extract = function
| OrOpExpr (e1, e2) -> extract e1 @ extract e2
| e -> [e] in
SwitchStatement ( e
, List.fold_right
(fun (a, b) acc -> ([a], b) :: acc) (cases)
(match default with None -> [] | Some stmts -> [ ([], stmts) ]))
from jingoo.
Thanks for reply. OrOpExpr
is not evaluated before building SwitchStatement
, and always generates list by extract
, then I'm OK.
EDIT: In this argument, I totally forgot about that expr isn't evaluated before statement is evaluated...
from jingoo.
Apparently {% case "a" case "b" %}
is hard to type, I agree with you.
Edit: I miss pushed the close issue button... but if more points are remain, re-open please.
from jingoo.
Related Issues (20)
- Usefulness of mutex? HOT 7
- Release trouble about jingoo 1.3.0 HOT 2
- Tests / Filters HOT 1
- Tset status HOT 5
- Truncated function expansion HOT 9
- Anonymous functions HOT 1
- Proposal: += operator HOT 1
- else if HOT 1
- null == null? HOT 2
- Functions and block statements HOT 1
- In Jingoo.Jg_template, provide a function for the getting variables HOT 33
- default filter is not usable HOT 8
- Use symbolic HTML entities rather than character codes for escaping HOT 5
- Too many frames created HOT 5
- PPX to generate typed TObj from records HOT 1
- No way to set models in built-in executable HOT 2
- May I ask how to do list appending on version 1.2.21 HOT 3
- Is there a way to pass data into template HOT 3
- urlencode filter HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jingoo.