source: EcnlProtoTool/trunk/webapp/webmrbc/Ruby/Logic.cs@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 3.2 KB
Line 
1// Porting from
2// https://github.com/jeanlazarou/blockly2ruby
3// Copyright (c) 2014 Jean Lazarou
4// MIT Lisence
5using System;
6using Bridge;
7using System.Collections.Generic;
8
9namespace WebMrbc
10{
11 partial class Ruby
12 {
13 public node controls_if(ControlsIfBlock block)
14 {
15 // If/elseif/else condition.
16 node code = null;
17 if (block.elseCount_ != 0) {
18 code = statementToCode(block, "ELSE");
19 if (code == null) code = new nil_node(this);
20 }
21 for (var n = block.elseifCount_; n >= 0; n--) {
22 var argument = valueToCode(block, "IF" + n);
23 if (argument == null) argument = new false_node(this);
24 var branch = statementToCode(block, "DO" + n);
25 code = new if_node(this, argument, branch, code, false);
26 }
27 return code;
28 }
29
30 public node logic_compare(LogicCompareBlock block)
31 {
32 // Comparison operator.
33 var OPERATORS = new Dictionary<string, string>() {
34 { "EQ", "==" },
35 { "NEQ", "!=" },
36 { "LT", "<" },
37 { "LTE", "<=" },
38 { "GT", ">" },
39 { "GTE", ">=" },
40 };
41 var @operator = OPERATORS[block.getFieldValue("OP")];
42 var argument0 = valueToCode(block, "A");
43 if (argument0 == null) argument0 = new int_node(this, 0);
44 var argument1 = valueToCode(block, "B");
45 if (argument1 == null) argument1 = new int_node(this, 0);
46 return new call_node(this, argument0, intern(@operator), argument1);
47 }
48
49 public node logic_operation(LogicOperationBlock block)
50 {
51 // Operations 'and', 'or'.
52 var @operator = (block.getFieldValue("OP") == "AND") ? "&&" : "||";
53 var argument0 = valueToCode(block, "A");
54 var argument1 = valueToCode(block, "B");
55 if (argument0 == null && argument1 == null) {
56 // If there are no arguments, then the return value is false.
57 argument0 = new false_node(this);
58 argument1 = new false_node(this);
59 }
60 else {
61 // Single missing arguments have no effect on the return value.
62 var defaultArgument = (@operator == "&&") ? (node)new true_node(this) : (node)new false_node(this);
63 if (argument0 == null) {
64 argument0 = defaultArgument;
65 }
66 if (argument1 == null) {
67 argument1 = defaultArgument;
68 }
69 }
70 if (@operator == "&&")
71 return new and_node(this, argument0, argument1);
72 else
73 return new or_node(this, argument0, argument1);
74 }
75
76 public node logic_negate(LogicNegateBlock block)
77 {
78 // Negation.
79 var argument0 = valueToCode(block, "BOOL");
80 if (argument0 == null) argument0 = new true_node(this);
81 return new call_node(this, argument0, intern("!"), (node)null);
82 }
83
84 public node logic_boolean(LogicBooleanBlock block)
85 {
86 // Boolean values true and false.
87 return (block.getFieldValue("BOOL") == "TRUE") ? (node)new true_node(this) : (node)new false_node(this);
88 }
89
90 public node logic_null(LogicNullBlock block)
91 {
92 // Null data type.
93 return new nil_node(this);
94 }
95
96 public node logic_ternary(LogicTernaryBlock block)
97 {
98 // Ternary operator.
99 var value_if = valueToCode(block, "IF");
100 if (value_if == null) value_if = new false_node(this);
101 var value_then = valueToCode(block, "THEN");
102 if (value_then == null) value_then = new nil_node(this);
103 var value_else = valueToCode(block, "ELSE");
104 if (value_else == null) value_else = new nil_node(this);
105 return new if_node(this, value_if, value_then, value_else, true);
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.