Twigでlispを作ってるんだナ! (9レス)
1-

1: 07/06(土)22:03 ID:pcJPW2j0N(1) AAS
{% macro evaluate(expression) %}
{% set result = _self._evaluate(expression|trim) %}{{ result }}
{% endmacro %}
{% macro _evaluate(expression) %}
{% set num = 1 %}
{# 前後の空白を取り除く #}{% set expr = expression|trim %}
{# カッコで挟まれた部分を識別する #}{% if expr starts with '(' and expr ends with ')' %}
{# 文字の部分だけをexprに代入 #}{% set expr = expr|slice(1, -1)|trim %}
{% endif %}
{# 空白で配列化 #}{% set components = expr|split(' ') %}
省20
2: 07/07(日)12:07 ID:Z9WWCnLov(1/8) AAS
{% macro evaluate(expression) %}{% set result = _self._evaluate(expression|trim) %}{{ result }}{% endmacro %}
{% macro _evaluate(expression) %}{% set num = 1 %}
{% set expr = expression|trim %}{% set expr = expr|split(')(') %}{% set expra = expr[0] %}
{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}
{% set operator = components[0] %}{% set operands = components[1:] %}
{% if operator == '+' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '-' %}{{_self._keisan(operator,operands)}}{% elseif operator == '*' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '/' %}{{_self._keisan(operator,operands)}}{% elseif operator == 'if' %}{% set a = operands[0]|trim('(') %}
{% set b = operands[1] %}{% set c = operands[2]|trim(')') %}{% if a == 'eq?' %}{% if b == c %}{% set result = "true" %}
{% set expra = expr[1] %}{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}
省7
3: 07/07(日)12:07 ID:Z9WWCnLov(2/8) AAS
{% macro _keisan(operator,operands) %}{% set num = 1 %}
{% if operator == '+' %}{% set result = operands|reduce((acc, item) => acc + item|number_format) %}{{result}}
{% elseif operator == '-' %}{% for item in operands %}{% if num == 1 %}{% set totalPrice = item|number_format %}
{% set num = num + 1 %}{% else %}{% set num = num + 1 %}{% set totalPrice = totalPrice - item|number_format %}{% endif %}
{% if num == operands|length + 1 %}{{ totalPrice }}{% endif %}{% endfor %}{% elseif operator == '*' %}{% set result = operands|reduce((acc, item) => acc * item|number_format,1) %}{{ result }}
{% elseif operator == '/' %}{% for item in operands %}
{% if num == 1 %}{% set totalPrice = item|number_format %}{% set num = num + 1 %}
{% else %}{% set num = num + 1 %}{% set totalPrice = totalPrice / item|number_format %}
{% endif %}{% if num == operands|length + 1 %}{{ totalPrice }}{% endif %}{% endfor %}{% endif %}{% endmacro %}
{{ _self.evaluate("(+ 1 2 6)") }}{{ _self.evaluate("(* 2 3 2)") }}
省2
4: 07/07(日)18:00 ID:Z9WWCnLov(3/8) AAS
{% macro evaluate(expression) %}{% set result = _self._evaluate(expression|trim) %}{{ result }} {% endmacro %}
{% macro _evaluate(expression) %}{% set num = 1 %}{% set i = 0 %}{% set expr = expression|trim %}{% set expr = expr|split(')(') %}
{% set expra = expr[0] %}{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}
{% set operator = components[0] %}{% set operands = components[1:] %}{% if operator == '+' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '-' %}{{_self._keisan(operator,operands)}} {% elseif operator == '*' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '/' %}{{_self._keisan(operator,operands)}} {% elseif operator == 'print' %}{{_self._keisan(operator,operands)}}
{% elseif operator == 'for' %}{% set a = operands[0]|trim('(') %}
{% set b = operands[1]|trim(')') %}{% if a == "range" and i < b|number_format %}{% for i in 1..b %}{% set expra = expr[1] %}
{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}{% set operator = components[0] %}
{% set operands = components[1:] %}{{_self._keisan(operator,operands)}} {% set i = i + 1 %}{% endfor %}{% endif %}
省3
5: 07/07(日)18:01 ID:Z9WWCnLov(4/8) AAS
{% else %}{% set result = "false" %}{% endif %}{% elseif a == 'neq?' %}{% if b != c %}{% set result = "true" %}{% set expra = expr[1] %}
{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}{% set operator = components[0] %}
{% set operands = components[1:] %}{{_self._keisan(operator,operands)}} {% else %}{% set result = "false" %}{% endif %}{% endif %}{% else %}
{% set result = "Unknown operation" %}{% endif %}{{ result }} {% endmacro %}{% macro _keisan(operator,operands) %}{% set num = 1 %}
{% if operator == '+' %}{% set result = operands|reduce((acc, item) => acc + item|number_format) %}{{ result }} {% elseif operator == '-' %}
{% for item in operands %}{% if num == 1 %}{% set totalPrice = item|number_format %}{% set num = num + 1 %}{% else %}{% set num = num + 1 %}
{% set totalPrice = totalPrice - item|number_format %}{% endif %}{% if num == operands|length + 1 %}{{ totalPrice }} {% endif %}{% endfor %}
{% elseif operator == '*' %}{% set result = operands|reduce((acc, item) => acc * item|number_format,1) %}{{ result }} {% elseif operator == '/' %}
{% for item in operands %}{% if num == 1 %}{% set totalPrice = item|number_format %}{% set num = num + 1 %}{% else %}{% set num = num + 1 %}
{% set totalPrice = totalPrice / item|number_format %}{% endif %}{% if num == operands|length + 1 %}{{ totalPrice }} {% endif %}{% endfor %}
省3
6: 07/07(日)18:04 ID:Z9WWCnLov(5/8) AAS
すごく辛い事もあったけど
ここまで作れたんだナ

ありがとうだナ
7: 07/07(日)19:33 ID:Z9WWCnLov(6/8) AAS
{% macro evaluate(expression) %}{% set result = _self._evaluate(expression|trim) %}{{ result }} {% endmacro %}
{% macro _evaluate(expression) %}{% set num = 1 %}{% set i = 0 %}{% set n1 = 1 %}{% set hai1 = [] %}{% set hai2 = [] %}
{% set expr = expression|trim %}{% set expr = expr|split(')(') %}
{% set expra = expr[0] %}{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}
{% set operator = components[0] %}{% set operands = components[1:] %}{% if operator == '+' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '-' %}{{_self._keisan(operator,operands)}} {% elseif operator == '*' %}{{_self._keisan(operator,operands)}}
{% elseif operator == '/' %}{{_self._keisan(operator,operands)}} {% elseif operator == 'print' %}{{_self._keisan(operator,operands)}}
{% elseif operator == 'set' %}{% set hai1 = operands[0]|trim('(') %}{% set hai2 = operands[1]|trim(')') %}{{hai1}}:{{hai2}}{% elseif operator == 'call' %}
{{ operands[0]|trim('(') in hai1 ? 'あった':'ない'}}{% elseif operator == 'for' %}{% set a = operands[0]|trim('(') %}
{% set b = operands[1]|trim(')') %}{% if a == "range" and i < b|number_format %}{% for i in 1..b %}{% set expra = expr[1] %}
省5
8: 07/07(日)19:34 ID:Z9WWCnLov(7/8) AAS
{% else %}{% set result = "false" %}{% endif %}{% elseif a == 'neq?' %}{% if b != c %}{% set result = "true" %}{% set expra = expr[1] %}
{% set expra = expra|trim('(')|trim(')')|trim %}{% set components = expra|split(' ') %}{% set operator = components[0] %}
{% set operands = components[1:] %}{{_self._keisan(operator,operands)}} {% else %}{% set result = "false" %}{% endif %}{% endif %}{% else %}
{% set result = "Unknown operation" %}{% endif %}{{ result }} {% endmacro %}{% macro _keisan(operator,operands) %}{% set num = 1 %}
{% if operator == '+' %}{% set result = operands|reduce((acc, item) => acc + item|number_format) %}{{ result }} {% elseif operator == '-' %}
{% for item in operands %}{% if num == 1 %}{% set totalPrice = item|number_format %}{% set num = num + 1 %}{% else %}{% set num = num + 1 %}
{% set totalPrice = totalPrice - item|number_format %}{% endif %}{% if num == operands|length + 1 %}{{ totalPrice }} {% endif %}{% endfor %}
{% elseif operator == '*' %}{% set result = operands|reduce((acc, item) => acc * item|number_format,1) %}{{ result }} {% elseif operator == '/' %}
{% for item in operands %}{% if num == 1 %}{% set totalPrice = item|number_format %}{% set num = num + 1 %}{% else %}{% set num = num + 1 %}
{% set totalPrice = totalPrice / item|number_format %}{% endif %}{% if num == operands|length + 1 %}{{ totalPrice }} {% endif %}{% endfor %}
省1
9: 07/07(日)19:37 ID:Z9WWCnLov(8/8) AAS
{{ _self.evaluate("(+ 1 2 6)") }}{{ _self.evaluate("(* 2 3 2)") }}{{ _self.evaluate("(- 10 3 2)") }} {{ _self.evaluate("(/ 15 3)") }}
{{ _self.evaluate("(if (eq? 3 3))(+ 1 2 6)") }}
{{ _self.evaluate("(if (neq? 3 1))(+ 1 2 6)") }}
{{ _self.evaluate("(for (range 6))(print doo") }}{{ _self.evaluate("(print shark)") }}{{ _self.evaluate("(set a1 1)") }}{{ _self.evaluate("(call a1)") }}

定義した変数が読み込めないし
配列の書き換えはできないんだナ

配列は一番後ろの値を取得すれば
いいけど
変数は難しいんだナ

テンプレートエンジンの勉強に使うと良いんだナ!
1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.408s*