Code Monkey home page Code Monkey logo

Comments (13)

tategakibunko avatar tategakibunko commented on June 8, 2024

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.

sagotch avatar sagotch commented on June 8, 2024

See geneanet@1d220ad for a first draft. It does not handle multiple case evaluating to same value yet.

from jingoo.

tategakibunko avatar tategakibunko commented on June 8, 2024

Thanks for reference, I've read it, compact and beautiful implementation!

from jingoo.

sagotch avatar sagotch commented on June 8, 2024

With geneanet@c026c28

You can use {% case foo || bar || baz %}. What do you think about this syntax?

from jingoo.

tategakibunko avatar tategakibunko commented on June 8, 2024

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.

sagotch avatar sagotch commented on June 8, 2024

I should have written

{% switch x %}
  {% case "foo" || "bar" || "baz" %}{{ ... }}
  {% default %} {{ ... }}
{% endswitch %}

from jingoo.

tategakibunko avatar tategakibunko commented on June 8, 2024

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.

tategakibunko avatar tategakibunko commented on June 8, 2024

But if it's only allowed for direct values, {% case "foo" || "bar" || "baz" %} seems short and good syntax.

from jingoo.

sagotch avatar sagotch commented on June 8, 2024

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.

tategakibunko avatar tategakibunko commented on June 8, 2024

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.

sagotch avatar sagotch commented on June 8, 2024

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.

tategakibunko avatar tategakibunko commented on June 8, 2024

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.

tategakibunko avatar tategakibunko commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.