rockit 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +24 -160
- data/TODO +17 -17
- data/VERSION +1 -1
- data/doc/rockit_paper.pdf +0 -0
- data/lib/packrat/grammar.rb +139 -84
- data/rakefile +27 -9
- data/tests/acceptance/packrat/java/atest_java.rb +37 -0
- data/tests/acceptance/packrat/java/java.rb +136 -0
- data/tests/acceptance/packrat/java/t.rb +10 -0
- data/tests/acceptance/packrat/java/todo +10 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/Java.rats +446 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaConstant.rats +111 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaCore.rats +508 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaIdentifier.rats +62 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaSymbol.rats +38 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaTree.rats +40 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/JavaType.rats +61 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/Spacing.rats +36 -0
- data/tests/acceptance/packrat/java/xtc.lang.java/Symbol.rats +77 -0
- data/tests/acceptance/packrat/minibasic/README +13 -0
- data/tests/acceptance/packrat/minibasic/atest_minibasic.rb +151 -13
- data/tests/acceptance/packrat/minibasic/minibasic.rb +94 -76
- data/tests/acceptance/packrat/minibasic/mult3.basic +6 -0
- data/tests/acceptance/packrat/minibasic/sumeven.basic +19 -0
- data/tests/unit/packrat/test_ast.rb +116 -0
- data/tests/unit/packrat/test_interpreting_parser.rb +15 -55
- metadata +22 -59
- data/lib/rockit/prettyprint/box.rb +0 -60
- data/lib/rockit/prettyprint/renderer.rb +0 -41
- data/lib/rockit/prettyprint/text_renderer.rb +0 -47
- data/lib/rockit/tree/base.rb +0 -223
- data/lib/rockit/tree/enter_leave_visitor.rb +0 -12
- data/lib/rockit/tree/graphviz.rb +0 -69
- data/lib/rockit/tree/visitor.rb +0 -12
- data/lib/util/array_alternatives.rb +0 -20
- data/lib/util/enter_leave_visitor.rb +0 -69
- data/lib/util/graphviz_dot.rb +0 -182
- data/lib/util/string_location.rb +0 -42
- data/lib/util/visitor.rb +0 -49
- data/lib/util/visitor_combinators.rb +0 -14
- data/tests/acceptance/rockit/dparser/atest_any_operator.rb +0 -33
- data/tests/acceptance/rockit/dparser/atest_arithmetic_grammar.rb +0 -30
- data/tests/acceptance/rockit/dparser/atest_list_operator.rb +0 -57
- data/tests/acceptance/rockit/dparser/atest_mult_operator.rb +0 -60
- data/tests/acceptance/rockit/dparser/atest_operator_grammar.rb +0 -61
- data/tests/acceptance/rockit/dparser/atest_plus_operator.rb +0 -55
- data/tests/acceptance/rockit/dparser/atest_samples_calculator.rb +0 -14
- data/tests/acceptance/rockit/dparser/atest_samples_minibasic.rb +0 -20
- data/tests/acceptance/rockit/dparser/atest_samples_multifunccalculator.rb +0 -36
- data/tests/acceptance/rockit/dparser/atest_simple_grammar.rb +0 -34
- data/tests/acceptance/rockit/dparser/atest_speculative_code_action.rb +0 -128
- data/tests/acceptance/rockit/dparser/calc_tests_common.rb +0 -103
- data/tests/unit/parse/utest_ebnf_grammar.rb +0 -50
- data/tests/unit/parse/utest_expand_grammar.rb +0 -23
- data/tests/unit/parse/utest_grammar.rb +0 -160
- data/tests/unit/rockit/assembler/llvm/utest_instructions.rb +0 -41
- data/tests/unit/rockit/assembler/llvm/utest_module.rb +0 -19
- data/tests/unit/rockit/prettyprint/utest_box.rb +0 -44
- data/tests/unit/rockit/tree/utest_tree_base.rb +0 -301
- data/tests/unit/rockit/tree/utest_tree_enter_leave_visitor.rb +0 -69
- data/tests/unit/rockit/tree/utest_tree_visitor.rb +0 -63
- data/tests/unit/rockit/utest_grammar.rb +0 -145
- data/tests/unit/rockit/utest_grammar_symbol.rb +0 -11
- data/tests/unit/rockit/utest_maybe_operator.rb +0 -12
- data/tests/unit/rockit/utest_regexp_terminal.rb +0 -45
- data/tests/unit/rockit/utest_repetition_operators.rb +0 -35
- data/tests/unit/rockit/utest_rule.rb +0 -23
- data/tests/unit/rockit/utest_string_terminal.rb +0 -40
- data/tests/unit/util/utest_array_alternatives.rb +0 -23
- data/tests/unit/util/utest_enter_leave_visitor.rb +0 -89
- data/tests/unit/util/utest_string_location.rb +0 -42
- data/tests/unit/util/utest_visitor.rb +0 -92
- data/tests/unit/util/utest_visitor_combinators.rb +0 -64
@@ -0,0 +1,62 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2005 Robert Grimm
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Definition of Java identifiers.
|
22
|
+
*
|
23
|
+
* @author Robert Grimm
|
24
|
+
* @version $Revision: 1.4 $
|
25
|
+
*/
|
26
|
+
module xtc.lang.JavaIdentifier(Symbol, Spacing);
|
27
|
+
|
28
|
+
import Symbol;
|
29
|
+
import Spacing;
|
30
|
+
|
31
|
+
body {
|
32
|
+
static {
|
33
|
+
reserve(new String[] {
|
34
|
+
"abstract", "default", "if", "private", "this",
|
35
|
+
"boolean", "do", "implements", "protected", "throw",
|
36
|
+
"break", "double", "import", "public", "throws",
|
37
|
+
"byte", "else", "instanceof", "return", "transient",
|
38
|
+
"case", "extends", "int", "short", "try",
|
39
|
+
"catch", "final", "interface", "static", "void",
|
40
|
+
"char", "finally", "long", "strictfp", "volatile",
|
41
|
+
"class", "float", "native", "super", "while",
|
42
|
+
"const", "for", "new", "switch",
|
43
|
+
"continue", "goto", "package", "synchronized"
|
44
|
+
});
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
option reserved;
|
49
|
+
|
50
|
+
generic QualifiedIdentifier =
|
51
|
+
Identifier ( void:".":Symbol Identifier )*
|
52
|
+
;
|
53
|
+
|
54
|
+
String Identifier =
|
55
|
+
yyValue:Word &{ ! RESERVED.contains(yyValue) } ;
|
56
|
+
|
57
|
+
String Word = WordCharacters Spacing ;
|
58
|
+
|
59
|
+
transient String WordCharacters =
|
60
|
+
start:_ &{ Character.isJavaIdentifierStart(start) }
|
61
|
+
( part:_ &{ Character.isJavaIdentifierPart(part) } )*
|
62
|
+
;
|
@@ -0,0 +1,38 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2005 Robert Grimm
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Definition of Java symbols.
|
22
|
+
*
|
23
|
+
* @author Robert Grimm
|
24
|
+
* @version $Revision: 1.2 $
|
25
|
+
*/
|
26
|
+
module xtc.lang.JavaSymbol(Symbol);
|
27
|
+
|
28
|
+
modify Symbol;
|
29
|
+
|
30
|
+
String SymbolCharacters +=
|
31
|
+
<TripleGreaterEqual> ">>>="
|
32
|
+
/ <GreaterGreaterEqual> ...
|
33
|
+
;
|
34
|
+
|
35
|
+
String SymbolCharacters +=
|
36
|
+
<TripleGreater> ">>>"
|
37
|
+
/ <GreaterGreater> ...
|
38
|
+
;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2004-2005 Robert Grimm, New York University
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Java grammar with AST support.
|
22
|
+
*
|
23
|
+
* @author Stacey Kuznetsov
|
24
|
+
* @author Robert Grimm
|
25
|
+
* @version $Revision: 1.7 $
|
26
|
+
*/
|
27
|
+
module xtc.lang.JavaTree;
|
28
|
+
|
29
|
+
instantiate xtc.lang.JavaType(xtc.lang.JavaIdentifier, xtc.lang.JavaSymbol);
|
30
|
+
instantiate xtc.lang.JavaConstant(xtc.lang.JavaIdentifier, xtc.util.Spacing);
|
31
|
+
instantiate xtc.lang.JavaIdentifier(xtc.lang.JavaSymbol, xtc.util.Spacing);
|
32
|
+
instantiate xtc.util.Symbol(xtc.util.Spacing);
|
33
|
+
instantiate xtc.lang.JavaSymbol(xtc.util.Symbol);
|
34
|
+
instantiate xtc.util.Spacing;
|
35
|
+
|
36
|
+
modify xtc.lang.JavaCore(xtc.lang.JavaType, xtc.lang.JavaConstant,
|
37
|
+
xtc.lang.JavaIdentifier, xtc.lang.JavaSymbol,
|
38
|
+
xtc.util.Spacing);
|
39
|
+
|
40
|
+
option withLocation, constant, parser(xtc.lang.JavaTreeParser);
|
@@ -0,0 +1,61 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2004-2005 Robert Grimm, New York University
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Definition of Java type names.
|
22
|
+
*
|
23
|
+
* @author Stacey Kuznetsov
|
24
|
+
* @author Robert Grimm
|
25
|
+
* @version $Revision: 1.3 $
|
26
|
+
*/
|
27
|
+
module xtc.lang.JavaType(Identifier, Symbol);
|
28
|
+
|
29
|
+
import Identifier;
|
30
|
+
import Symbol;
|
31
|
+
|
32
|
+
GNode ResultType =
|
33
|
+
<Void> VoidTypeSpecifier
|
34
|
+
/ <Regular> Type
|
35
|
+
;
|
36
|
+
|
37
|
+
generic VoidTypeSpecifier = <Void> void:"void":Word;
|
38
|
+
|
39
|
+
generic Type =
|
40
|
+
<Type> TypeName Dimensions?
|
41
|
+
;
|
42
|
+
|
43
|
+
generic TypeName =
|
44
|
+
<Basic> BasicType
|
45
|
+
/ <Qualified> yyValue:QualifiedIdentifier
|
46
|
+
;
|
47
|
+
|
48
|
+
String BasicType =
|
49
|
+
<Byte> "byte":Word
|
50
|
+
/ <Short> "short":Word
|
51
|
+
/ <Char> "char":Word
|
52
|
+
/ <Int> "int":Word
|
53
|
+
/ <Long> "long":Word
|
54
|
+
/ <Float> "float":Word
|
55
|
+
/ <Double> "double":Word
|
56
|
+
/ <Boolean> "boolean":Word
|
57
|
+
;
|
58
|
+
|
59
|
+
generic Dimensions =
|
60
|
+
( "[":Symbol void:"]":Symbol )+
|
61
|
+
;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2005 Robert Grimm
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Definition of spacing, including C comments.
|
22
|
+
*
|
23
|
+
* @author Robert Grimm
|
24
|
+
* @version $Revision: 1.3 $
|
25
|
+
*/
|
26
|
+
module xtc.util.Spacing;
|
27
|
+
|
28
|
+
transient void Spacing =
|
29
|
+
( Space / LineTerminator / TraditionalComment / EndOfLineComment )*
|
30
|
+
;
|
31
|
+
|
32
|
+
transient void Space = ' ' / '\t' / '\f' ;
|
33
|
+
transient void TraditionalComment = "/*" ( '*' !'/' / !'*' _ )* "*/" ;
|
34
|
+
transient void EndOfLineComment = "//" ( ![\n\r] _ )* LineTerminator ;
|
35
|
+
transient void LineTerminator = '\r' '\n' / '\r' / '\n' ;
|
36
|
+
transient void EndOfFile = !_ ;
|
@@ -0,0 +1,77 @@
|
|
1
|
+
/*
|
2
|
+
* xtc - The eXTensible Compiler
|
3
|
+
* Copyright (C) 2005 Robert Grimm
|
4
|
+
*
|
5
|
+
* This program is free software; you can redistribute it and/or
|
6
|
+
* modify it under the terms of the GNU General Public License
|
7
|
+
* as published by the Free Software Foundation; either version 2
|
8
|
+
* of the License, or (at your option) any later version.
|
9
|
+
*
|
10
|
+
* This program is distributed in the hope that it will be useful,
|
11
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
* GNU General Public License for more details.
|
14
|
+
*
|
15
|
+
* You should have received a copy of the GNU General Public License
|
16
|
+
* along with this program; if not, write to the Free Software
|
17
|
+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
*/
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Definition of symbols common to C and Java.
|
22
|
+
*
|
23
|
+
* @author Robert Grimm
|
24
|
+
* @version $Revision: 1.6 $
|
25
|
+
*/
|
26
|
+
module xtc.util.Symbol(Spacing);
|
27
|
+
|
28
|
+
import Spacing;
|
29
|
+
|
30
|
+
String Symbol = SymbolCharacters Spacing ;
|
31
|
+
|
32
|
+
transient String SymbolCharacters =
|
33
|
+
<GreaterGreaterEqual> ">>="
|
34
|
+
/ <LessLessEqual> "<<="
|
35
|
+
/ <GreaterGreater> ">>"
|
36
|
+
/ <LessLess> "<<"
|
37
|
+
/ <PlusEqual> "+="
|
38
|
+
/ <MinusEqual> "-="
|
39
|
+
/ <StarEqual> "*="
|
40
|
+
/ <SlashEqual> "/="
|
41
|
+
/ <PercentEqual> "%="
|
42
|
+
/ <AmpersandEqual> "&="
|
43
|
+
/ <CaretEqual> "^="
|
44
|
+
/ <BarEqual> "|="
|
45
|
+
/ <PlusPlus> "++"
|
46
|
+
/ <MinusMinus> "--"
|
47
|
+
/ <AmpersandAmpersand> "&&"
|
48
|
+
/ <BarBar> "||"
|
49
|
+
/ <LessEqual> "<="
|
50
|
+
/ <GreaterEqual> ">="
|
51
|
+
/ <EqualEqual> "=="
|
52
|
+
/ <BangEqual> "!="
|
53
|
+
/ <Semicolon> ";"
|
54
|
+
/ <Colon> ":"
|
55
|
+
/ <Comma> ","
|
56
|
+
/ <Period> "."
|
57
|
+
/ <OpenBrace> "{"
|
58
|
+
/ <CloseBrace> "}"
|
59
|
+
/ <OpenParenthesis> "("
|
60
|
+
/ <CloseParenthesis> ")"
|
61
|
+
/ <OpenBracket> "["
|
62
|
+
/ <CloseBracket> "]"
|
63
|
+
/ <Bang> "!"
|
64
|
+
/ <Tilde> "~"
|
65
|
+
/ <Plus> "+"
|
66
|
+
/ <Minus> "-"
|
67
|
+
/ <Star> "*"
|
68
|
+
/ <Slash> "/"
|
69
|
+
/ <Percent> "%"
|
70
|
+
/ <Less> "<"
|
71
|
+
/ <Greater> ">"
|
72
|
+
/ <Equal> "="
|
73
|
+
/ <Ampersand> "&"
|
74
|
+
/ <Caret> "^"
|
75
|
+
/ <Bar> "|"
|
76
|
+
/ <Question> "?"
|
77
|
+
;
|
@@ -2,7 +2,7 @@ require 'test/unit'
|
|
2
2
|
|
3
3
|
require File.join(File.dirname(__FILE__), "minibasic")
|
4
4
|
|
5
|
-
class
|
5
|
+
class ATestMiniBasicParse < Test::Unit::TestCase
|
6
6
|
def assert_parse(exp, str)
|
7
7
|
res = MiniBasic::Parser.parse_string(str)
|
8
8
|
assert_equal(exp, res)
|
@@ -15,31 +15,169 @@ class ATestMiniBasic < Test::Unit::TestCase
|
|
15
15
|
include MiniBasic::Grammar::ASTs
|
16
16
|
|
17
17
|
def test_01
|
18
|
-
assert_stmt(PrintLn[
|
18
|
+
assert_stmt(PrintLn[], "PRINTLN")
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_02
|
22
|
-
assert_stmt(Read[
|
22
|
+
assert_stmt(Read[:A], "READ A")
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_03
|
26
|
-
assert_stmt(Print[
|
26
|
+
assert_stmt(Print[:BC], "PRINT BC")
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_04
|
30
|
-
assert_stmt(Assign[:D,
|
31
|
-
assert_stmt(Assign[:D,
|
32
|
-
assert_stmt(Assign[:D,
|
30
|
+
assert_stmt(Assign[:D, 1], "D := 1")
|
31
|
+
assert_stmt(Assign[:D, 1], "D :=1")
|
32
|
+
assert_stmt(Assign[:D, 1], "D:=1")
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_05
|
36
|
-
assert_stmt(For[
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
"NEXT"], "FOR A := 1 TO 3 S:= S + A PRINT S NEXT")
|
36
|
+
assert_stmt(For[:A, 1, 3,
|
37
|
+
[Assign[:S, BinExpr[:S, "+", :A]],
|
38
|
+
Print[:S]]],
|
39
|
+
"FOR A := 1 TO 3 S:= S + A PRINT S NEXT")
|
41
40
|
end
|
42
41
|
|
43
|
-
def
|
42
|
+
def test_06_if_without_else
|
43
|
+
assert_stmt(If[Cond[3, ">", 1], [PrintLn[]], nil],
|
44
|
+
"IF 3 > 1 THEN PRINTLN ENDIF")
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_06_if_with_else
|
48
|
+
assert_stmt(If[Cond[3, ">", 1],
|
49
|
+
[Print[:A], PrintLn[]],
|
50
|
+
[Print[:B], PrintLn[]]],
|
51
|
+
"IF 3 > 1 THEN PRINT A PRINTLN ELSE PRINT B PRINTLN ENDIF")
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_07_string
|
55
|
+
assert_stmt(Assign[:A, "H"], "A := \"H\"")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class ATestMiniBasicInterpret < Test::Unit::TestCase
|
60
|
+
def interpret(prg, options = {})
|
61
|
+
i = MiniBasic::Interpreter.new(options)
|
62
|
+
ast = MiniBasic::Parser.parse_string(prg)
|
63
|
+
res = i.interpret(ast)
|
64
|
+
return i, res, ast
|
65
|
+
end
|
66
|
+
|
67
|
+
def interp(prg, options = {})
|
68
|
+
interpret(prg, options).first
|
69
|
+
end
|
70
|
+
|
71
|
+
NoIndata = ("__" + rand(1e10).to_s).intern
|
72
|
+
|
73
|
+
def test_01_assignment
|
74
|
+
i = interp("A:=1")
|
75
|
+
assert_equal({:A => 1}, i.vars)
|
76
|
+
|
77
|
+
i = interp("A:=1 B := 2 C:= 3 D := 4")
|
78
|
+
assert_equal({:A => 1, :B => 2, :C => 3, :D => 4},
|
79
|
+
i.vars)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_02_if
|
83
|
+
i = interp("A := 1 IF A > 1 THEN A := 1 ELSE A := 0 ENDIF")
|
84
|
+
assert_equal({:A => 0}, i.vars)
|
85
|
+
|
86
|
+
i = interp("A := 1 IF A = 1 THEN B := 2 ELSE A := 0 ENDIF")
|
87
|
+
assert_equal({:A => 1, :B => 2}, i.vars)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_03_for
|
91
|
+
i = interp("S := 1 FOR I := 1 TO 4 S := S + I NEXT")
|
92
|
+
assert_equal({:S => 11, :I => 4}, i.vars)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_04_string
|
96
|
+
i = interp('A := "Hello World!"')
|
97
|
+
assert_equal({:A => "Hello World!"}, i.vars)
|
98
|
+
end
|
99
|
+
|
100
|
+
class MockStdin
|
101
|
+
def initialize(*lines)
|
102
|
+
@lines, @cnt = lines, -1
|
103
|
+
end
|
104
|
+
def gets; @lines[(@cnt += 1)]; end
|
105
|
+
end
|
106
|
+
|
107
|
+
class MockStdout
|
108
|
+
attr_reader :strs
|
109
|
+
def initialize
|
110
|
+
@strs = []
|
111
|
+
end
|
112
|
+
def print(str); @strs << str; end
|
113
|
+
def flush; end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_05_read
|
117
|
+
i = interp("READ A", :stdin => MockStdin.new("452"),
|
118
|
+
:stdout => (so = MockStdout.new))
|
119
|
+
assert_equal({:A => 452}, i.vars)
|
120
|
+
assert_equal(["? "], so.strs)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_06_print
|
124
|
+
i = interp("A := 1 PRINT A", :stdout => (so = MockStdout.new))
|
125
|
+
assert_equal({:A => 1}, i.vars)
|
126
|
+
assert_equal(["1"], so.strs)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_07_println
|
130
|
+
i = interp("PRINTLN", :stdout => (so = MockStdout.new))
|
131
|
+
assert_equal({}, i.vars)
|
132
|
+
assert_equal(["\n"], so.strs)
|
133
|
+
end
|
134
|
+
|
135
|
+
def assert_prg(filename, stdInLines, expStdoutStrs)
|
136
|
+
fn = File.join(File.dirname(__FILE__), filename)
|
137
|
+
prg = File.open(fn) {|fh| fh.read}
|
138
|
+
stdout = MockStdout.new
|
139
|
+
stdin = MockStdin.new(*stdInLines)
|
140
|
+
i = interp(prg, :stdout => stdout, :stdin => stdin)
|
141
|
+
assert_equal(expStdoutStrs, stdout.strs)
|
142
|
+
end
|
143
|
+
|
144
|
+
def assert_mult3(input, out)
|
145
|
+
ao =
|
146
|
+
["What is your number", "? ", input.to_s, " * 3 = ", out.to_s, "\n"]
|
147
|
+
assert_prg("mult3.basic", [input.to_s], ao)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_08_mult3
|
151
|
+
assert_mult3(0, 0)
|
152
|
+
assert_mult3(1, 3)
|
153
|
+
assert_mult3(2, 6)
|
154
|
+
assert_mult3(10, 30)
|
155
|
+
end
|
156
|
+
|
157
|
+
def assert_sumeven(input, out)
|
158
|
+
ao =
|
159
|
+
[
|
160
|
+
"I can sum even numbers.", "\n",
|
161
|
+
"At what number should I start summing", "? ",
|
162
|
+
"At what number should I stop", "? ",
|
163
|
+
"The sum of all even numbers between (inclusive) ",
|
164
|
+
]
|
165
|
+
ao << input[0].to_s
|
166
|
+
ao << " and "
|
167
|
+
ao << input[1].to_s
|
168
|
+
ao << " is = "
|
169
|
+
ao << out.to_s
|
170
|
+
ao << "\n"
|
171
|
+
assert_prg("sumeven.basic", input.map {|i| i.to_s}, ao)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_09_sumeven_program_file
|
175
|
+
assert_sumeven([0, 1], 0)
|
176
|
+
assert_sumeven([1, 1], 0)
|
177
|
+
assert_sumeven([1, 4], 6)
|
178
|
+
assert_sumeven([1, 5], 6)
|
179
|
+
assert_sumeven([1, 6], 12)
|
180
|
+
assert_sumeven([1, 8], 20)
|
181
|
+
assert_sumeven([1, 10], 30)
|
44
182
|
end
|
45
183
|
end
|