finitio 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +66 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +34 -30
- data/finitio.gemspec +1 -1
- data/lib/finitio.rb +7 -39
- data/lib/finitio/errors.rb +14 -0
- data/lib/finitio/support.rb +4 -1
- data/lib/finitio/support/attribute.rb +13 -7
- data/lib/finitio/support/compilation.rb +55 -0
- data/lib/finitio/support/constraint.rb +46 -0
- data/lib/finitio/support/contract.rb +33 -0
- data/lib/finitio/support/dress_helper.rb +3 -2
- data/lib/finitio/support/heading.rb +57 -17
- data/lib/finitio/support/metadata.rb +20 -0
- data/lib/finitio/support/type_factory.rb +154 -41
- data/lib/finitio/syntax.rb +16 -31
- data/lib/finitio/syntax/expr.rb +56 -0
- data/lib/finitio/syntax/expr/arith_op.rb +22 -0
- data/lib/finitio/syntax/expr/comparison.rb +22 -0
- data/lib/finitio/syntax/expr/fn_call.rb +24 -0
- data/lib/finitio/syntax/expr/identifier.rb +18 -0
- data/lib/finitio/syntax/expr/literal.rb +19 -0
- data/lib/finitio/syntax/expr/logic_dyadic.rb +22 -0
- data/lib/finitio/syntax/expr/logic_not.rb +21 -0
- data/lib/finitio/syntax/expr/oo_call.rb +24 -0
- data/lib/finitio/syntax/expr/parenthesized.rb +20 -0
- data/lib/finitio/syntax/expr/unary_minus_op.rb +21 -0
- data/lib/finitio/syntax/expressions.citrus +129 -0
- data/lib/finitio/syntax/finitio.citrus +10 -208
- data/lib/finitio/syntax/finitio.sexp +5 -1
- data/lib/finitio/syntax/lexer.citrus +91 -0
- data/lib/finitio/syntax/literal.rb +16 -0
- data/lib/finitio/syntax/literal/boolean.rb +22 -0
- data/lib/finitio/syntax/literal/integer.rb +14 -0
- data/lib/finitio/syntax/literal/real.rb +14 -0
- data/lib/finitio/syntax/literal/string.rb +14 -0
- data/lib/finitio/syntax/literals.citrus +44 -0
- data/lib/finitio/syntax/node.rb +53 -0
- data/lib/finitio/syntax/type.rb +28 -0
- data/lib/finitio/syntax/type/ad_type.rb +29 -0
- data/lib/finitio/syntax/{any_type.rb → type/any_type.rb} +1 -1
- data/lib/finitio/syntax/type/attribute.rb +33 -0
- data/lib/finitio/syntax/{builtin_type.rb → type/builtin_type.rb} +3 -1
- data/lib/finitio/syntax/{constraint_def.rb → type/constraint_def.rb} +4 -3
- data/lib/finitio/syntax/type/constraints.rb +17 -0
- data/lib/finitio/syntax/{contract.rb → type/contract.rb} +9 -3
- data/lib/finitio/syntax/{definitions.rb → type/definitions.rb} +1 -0
- data/lib/finitio/syntax/{expression.rb → type/expression.rb} +2 -1
- data/lib/finitio/syntax/{external_pair.rb → type/external_pair.rb} +3 -1
- data/lib/finitio/syntax/type/heading.rb +30 -0
- data/lib/finitio/syntax/{inline_pair.rb → type/inline_pair.rb} +3 -1
- data/lib/finitio/syntax/{lambda_expr.rb → type/lambda_expr.rb} +3 -0
- data/lib/finitio/syntax/type/main_type.rb +18 -0
- data/lib/finitio/syntax/type/metadata.rb +18 -0
- data/lib/finitio/syntax/type/metadata_attr.rb +15 -0
- data/lib/finitio/syntax/{named_constraint.rb → type/named_constraint.rb} +10 -3
- data/lib/finitio/syntax/type/relation_type.rb +24 -0
- data/lib/finitio/syntax/{seq_type.rb → type/seq_type.rb} +3 -0
- data/lib/finitio/syntax/{set_type.rb → type/set_type.rb} +3 -0
- data/lib/finitio/syntax/type/struct_type.rb +17 -0
- data/lib/finitio/syntax/{sub_type.rb → type/sub_type.rb} +3 -0
- data/lib/finitio/syntax/{system.rb → type/system.rb} +5 -4
- data/lib/finitio/syntax/type/tuple_type.rb +24 -0
- data/lib/finitio/syntax/{type_def.rb → type/type_def.rb} +8 -3
- data/lib/finitio/syntax/{type_ref.rb → type/type_ref.rb} +4 -1
- data/lib/finitio/syntax/{union_type.rb → type/union_type.rb} +1 -0
- data/lib/finitio/syntax/{unnamed_constraint.rb → type/unnamed_constraint.rb} +7 -1
- data/lib/finitio/syntax/types.citrus +196 -0
- data/lib/finitio/system.rb +15 -15
- data/lib/finitio/type.rb +38 -6
- data/lib/finitio/type/ad_type.rb +41 -18
- data/lib/finitio/type/alias_type.rb +37 -0
- data/lib/finitio/type/any_type.rb +7 -3
- data/lib/finitio/type/builtin_type.rb +5 -4
- data/lib/finitio/{support → type}/collection_type.rb +3 -4
- data/lib/finitio/type/hash_based_type.rb +91 -0
- data/lib/finitio/type/heading_based_type.rb +28 -0
- data/lib/finitio/type/multi_relation_type.rb +34 -0
- data/lib/finitio/type/multi_tuple_type.rb +29 -0
- data/lib/finitio/type/proxy_type.rb +40 -0
- data/lib/finitio/type/rel_based_type.rb +42 -0
- data/lib/finitio/type/relation_type.rb +6 -47
- data/lib/finitio/type/seq_type.rb +4 -0
- data/lib/finitio/type/set_type.rb +4 -0
- data/lib/finitio/type/struct_type.rb +84 -0
- data/lib/finitio/type/sub_type.rb +25 -22
- data/lib/finitio/type/tuple_type.rb +6 -57
- data/lib/finitio/type/union_type.rb +12 -5
- data/lib/finitio/version.rb +2 -2
- data/spec/{unit/attribute → attribute}/test_equality.rb +8 -5
- data/spec/{unit/attribute → attribute}/test_fetch_on.rb +5 -5
- data/spec/attribute/test_initialize.rb +26 -0
- data/spec/attribute/test_optional.rb +18 -0
- data/spec/attribute/test_required.rb +18 -0
- data/spec/attribute/test_to_name.rb +20 -0
- data/spec/constraint/test_anonymous.rb +20 -0
- data/spec/constraint/test_equality.rb +39 -0
- data/spec/constraint/test_name.rb +20 -0
- data/spec/constraint/test_named.rb +20 -0
- data/spec/constraint/test_triple_equal.rb +13 -0
- data/spec/{unit/qrb/system.q → finitio/system.fio} +0 -0
- data/spec/{unit/qrb → finitio}/test_ast.rb +0 -0
- data/spec/finitio/test_parse.rb +23 -0
- data/spec/{unit/qrb/test_parse.rb → finitio/test_system.rb} +6 -6
- data/spec/heading/test_allow_extra.rb +24 -0
- data/spec/{unit/heading → heading}/test_each.rb +2 -2
- data/spec/heading/test_equality.rb +38 -0
- data/spec/heading/test_hash.rb +38 -0
- data/spec/heading/test_hash_get.rb +17 -0
- data/spec/{unit/heading → heading}/test_initialize.rb +2 -2
- data/spec/heading/test_multi.rb +57 -0
- data/spec/{unit/heading → heading}/test_size.rb +0 -0
- data/spec/heading/test_to_name.rb +58 -0
- data/spec/spec_helper.rb +17 -1
- data/spec/syntax/expr/test_free_variables.rb +46 -0
- data/spec/syntax/expr/test_to_proc_source.rb +43 -0
- data/spec/{unit/syntax → syntax}/nodes/test_ad_type.rb +26 -26
- data/spec/{unit/syntax → syntax}/nodes/test_any_type.rb +2 -2
- data/spec/syntax/nodes/test_attribute.rb +65 -0
- data/spec/{unit/syntax → syntax}/nodes/test_builtin_type.rb +5 -5
- data/spec/{unit/syntax → syntax}/nodes/test_comment.rb +1 -1
- data/spec/{unit/syntax → syntax}/nodes/test_constraint_def.rb +3 -8
- data/spec/{unit/syntax → syntax}/nodes/test_constraints.rb +21 -16
- data/spec/{unit/syntax → syntax}/nodes/test_contract.rb +28 -35
- data/spec/{unit/syntax → syntax}/nodes/test_expression.rb +13 -5
- data/spec/{unit/syntax → syntax}/nodes/test_heading.rb +25 -7
- data/spec/syntax/nodes/test_metadata.rb +28 -0
- data/spec/{unit/syntax → syntax}/nodes/test_named_constraint.rb +8 -8
- data/spec/syntax/nodes/test_relation_type.rb +84 -0
- data/spec/{unit/syntax → syntax}/nodes/test_seq_type.rb +4 -4
- data/spec/{unit/syntax → syntax}/nodes/test_set_type.rb +4 -4
- data/spec/{unit/syntax → syntax}/nodes/test_spacing.rb +1 -1
- data/spec/syntax/nodes/test_struct_type.rb +38 -0
- data/spec/{unit/syntax → syntax}/nodes/test_sub_type.rb +14 -14
- data/spec/{unit/syntax → syntax}/nodes/test_system.rb +3 -3
- data/spec/syntax/nodes/test_tuple_type.rb +94 -0
- data/spec/syntax/nodes/test_type_def.rb +57 -0
- data/spec/{unit/syntax → syntax}/nodes/test_type_ref.rb +3 -3
- data/spec/{unit/syntax → syntax}/nodes/test_union_type.rb +3 -3
- data/spec/{unit/syntax → syntax}/nodes/test_unnamed_constraint.rb +7 -7
- data/spec/syntax/test_compile.rb +41 -0
- data/spec/{unit/syntax → syntax}/test_compile_type.rb +1 -1
- data/spec/{unit/system → system}/test_add_type.rb +6 -6
- data/spec/{unit/system → system}/test_dsl.rb +2 -2
- data/spec/{unit/system → system}/test_dup.rb +4 -4
- data/spec/{unit/system → system}/test_fetch.rb +4 -4
- data/spec/{unit/system → system}/test_get_type.rb +2 -2
- data/spec/{unit/system → system}/test_initialize.rb +0 -0
- data/spec/test_finitio.rb +8 -0
- data/spec/{unit/type → type}/ad_type/test_default_name.rb +1 -2
- data/spec/{unit/type → type}/ad_type/test_dress.rb +19 -9
- data/spec/{unit/type → type}/ad_type/test_include.rb +3 -3
- data/spec/{unit/type → type}/ad_type/test_initialize.rb +15 -8
- data/spec/{unit/type → type}/ad_type/test_name.rb +2 -2
- data/spec/type/alias_type/test_default_name.rb +10 -0
- data/spec/type/alias_type/test_delegation.rb +29 -0
- data/spec/type/alias_type/test_name.rb +10 -0
- data/spec/{unit/type → type}/any_type/test_default_name.rb +1 -1
- data/spec/{unit/type → type}/any_type/test_dress.rb +0 -0
- data/spec/{unit/type → type}/any_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/any_type/test_include.rb +2 -2
- data/spec/{unit/type → type}/any_type/test_initialize.rb +0 -0
- data/spec/{unit/type → type}/any_type/test_name.rb +2 -2
- data/spec/{unit/type → type}/builtin_type/test_default_name.rb +1 -1
- data/spec/{unit/type → type}/builtin_type/test_dress.rb +3 -3
- data/spec/{unit/type → type}/builtin_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/builtin_type/test_include.rb +2 -2
- data/spec/{unit/type → type}/builtin_type/test_initialize.rb +1 -1
- data/spec/{unit/type → type}/builtin_type/test_name.rb +2 -2
- data/spec/type/multi_relation_type/test_default_name.rb +19 -0
- data/spec/type/multi_relation_type/test_dress.rb +206 -0
- data/spec/type/multi_relation_type/test_equality.rb +36 -0
- data/spec/type/multi_relation_type/test_include.rb +89 -0
- data/spec/type/multi_relation_type/test_initialize.rb +29 -0
- data/spec/type/multi_relation_type/test_name.rb +27 -0
- data/spec/type/multi_tuple_type/test_default_name.rb +17 -0
- data/spec/type/multi_tuple_type/test_dress.rb +146 -0
- data/spec/type/multi_tuple_type/test_equality.rb +32 -0
- data/spec/type/multi_tuple_type/test_include.rb +73 -0
- data/spec/type/multi_tuple_type/test_initialize.rb +30 -0
- data/spec/type/multi_tuple_type/test_name.rb +24 -0
- data/spec/type/proxy_type/test_delegation.rb +37 -0
- data/spec/type/proxy_type/test_resolve.rb +29 -0
- data/spec/{unit/type → type}/relation_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/relation_type/test_dress.rb +24 -24
- data/spec/{unit/type → type}/relation_type/test_equality.rb +6 -6
- data/spec/{unit/type → type}/relation_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/relation_type/test_initialize.rb +2 -2
- data/spec/{unit/type → type}/relation_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/seq_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/seq_type/test_dress.rb +5 -5
- data/spec/{unit/type → type}/seq_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/seq_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/seq_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/seq_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/set_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/set_type/test_dress.rb +8 -8
- data/spec/{unit/type → type}/set_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/set_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/set_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/set_type/test_name.rb +0 -0
- data/spec/type/struct_type/test_default_name.rb +10 -0
- data/spec/type/struct_type/test_dress.rb +105 -0
- data/spec/type/struct_type/test_equality.rb +28 -0
- data/spec/type/struct_type/test_include.rb +40 -0
- data/spec/type/struct_type/test_initialize.rb +22 -0
- data/spec/type/struct_type/test_name.rb +20 -0
- data/spec/{unit/type → type}/sub_type/test_default_name.rb +2 -2
- data/spec/{unit/type → type}/sub_type/test_dress.rb +14 -14
- data/spec/type/sub_type/test_equality.rb +46 -0
- data/spec/{unit/type → type}/sub_type/test_include.rb +6 -6
- data/spec/type/sub_type/test_initialize.rb +13 -0
- data/spec/{unit/type → type}/sub_type/test_name.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/tuple_type/test_dress.rb +18 -18
- data/spec/{unit/type → type}/tuple_type/test_equality.rb +6 -6
- data/spec/{unit/type → type}/tuple_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_initialize.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/union_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/union_type/test_dress.rb +7 -6
- data/spec/{unit/type → type}/union_type/test_equality.rb +7 -7
- data/spec/{unit/type → type}/union_type/test_include.rb +3 -3
- data/spec/{unit/type → type}/union_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/union_type/test_name.rb +0 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_adt.rb +4 -4
- data/spec/{unit/type_factory → type_factory}/dsl/test_any.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/dsl/test_attribute.rb +16 -2
- data/spec/{unit/type_factory → type_factory}/dsl/test_attributes.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/dsl/test_builtin.rb +3 -3
- data/spec/type_factory/dsl/test_multi_relation.rb +39 -0
- data/spec/type_factory/dsl/test_multi_tuple.rb +37 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_relation.rb +6 -6
- data/spec/{unit/type_factory → type_factory}/dsl/test_seq.rb +4 -4
- data/spec/{unit/type_factory → type_factory}/dsl/test_set.rb +4 -4
- data/spec/type_factory/dsl/test_struct.rb +45 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_subtype.rb +10 -8
- data/spec/{unit/type_factory → type_factory}/dsl/test_tuple.rb +5 -5
- data/spec/{unit/type_factory → type_factory}/dsl/test_union.rb +6 -6
- data/spec/{unit/type_factory → type_factory}/factory/test_builtin.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/factory/test_seq_type.rb +2 -2
- data/spec/{unit/type_factory → type_factory}/factory/test_set_type.rb +2 -2
- data/spec/type_factory/factory/test_struct_type.rb +18 -0
- data/spec/{unit/type_factory → type_factory}/factory/test_sub_type.rb +7 -7
- data/spec/{unit/type_factory → type_factory}/factory/test_tuple_type.rb +4 -4
- metadata +398 -286
- data/lib/finitio/data_type.rb +0 -29
- data/lib/finitio/syntax/ad_type.rb +0 -32
- data/lib/finitio/syntax/attribute.rb +0 -15
- data/lib/finitio/syntax/constraints.rb +0 -22
- data/lib/finitio/syntax/heading.rb +0 -19
- data/lib/finitio/syntax/relation_type.rb +0 -15
- data/lib/finitio/syntax/support.rb +0 -13
- data/lib/finitio/syntax/tuple_type.rb +0 -15
- data/spec/acceptance/Finitio/test_default.rb +0 -96
- data/spec/acceptance/Finitio/test_parsing.rb +0 -15
- data/spec/acceptance/ad_type/test_in_finitio.rb +0 -82
- data/spec/acceptance/ad_type/test_in_ruby.rb +0 -60
- data/spec/unit/attribute/test_initialize.rb +0 -13
- data/spec/unit/attribute/test_to_name.rb +0 -10
- data/spec/unit/heading/test_equality.rb +0 -28
- data/spec/unit/heading/test_to_name.rb +0 -32
- data/spec/unit/syntax/nodes/test_attribute.rb +0 -38
- data/spec/unit/syntax/nodes/test_relation_type.rb +0 -59
- data/spec/unit/syntax/nodes/test_tuple_type.rb +0 -59
- data/spec/unit/syntax/nodes/test_type_def.rb +0 -33
- data/spec/unit/test_finitio.rb +0 -15
- data/spec/unit/type/sub_type/test_equality.rb +0 -34
- data/spec/unit/type/sub_type/test_initialize.rb +0 -16
data/lib/finitio/syntax.rb
CHANGED
@@ -1,32 +1,14 @@
|
|
1
1
|
require 'citrus'
|
2
|
-
require_relative 'syntax/
|
3
|
-
require_relative 'syntax/
|
4
|
-
require_relative 'syntax/
|
5
|
-
require_relative 'syntax/
|
6
|
-
require_relative 'syntax/expression'
|
7
|
-
require_relative 'syntax/attribute'
|
8
|
-
require_relative 'syntax/heading'
|
9
|
-
require_relative 'syntax/any_type'
|
10
|
-
require_relative 'syntax/builtin_type'
|
11
|
-
require_relative 'syntax/sub_type'
|
12
|
-
require_relative 'syntax/constraint_def'
|
13
|
-
require_relative 'syntax/constraints'
|
14
|
-
require_relative 'syntax/named_constraint'
|
15
|
-
require_relative 'syntax/unnamed_constraint'
|
16
|
-
require_relative 'syntax/seq_type'
|
17
|
-
require_relative 'syntax/set_type'
|
18
|
-
require_relative 'syntax/tuple_type'
|
19
|
-
require_relative 'syntax/relation_type'
|
20
|
-
require_relative 'syntax/union_type'
|
21
|
-
require_relative 'syntax/type_ref'
|
22
|
-
require_relative 'syntax/ad_type'
|
23
|
-
require_relative 'syntax/contract'
|
24
|
-
require_relative 'syntax/inline_pair'
|
25
|
-
require_relative 'syntax/external_pair'
|
26
|
-
require_relative 'syntax/lambda_expr'
|
2
|
+
require_relative 'syntax/node'
|
3
|
+
require_relative 'syntax/literal'
|
4
|
+
require_relative 'syntax/expr'
|
5
|
+
require_relative 'syntax/type'
|
27
6
|
module Finitio
|
28
7
|
module Syntax
|
29
8
|
|
9
|
+
Citrus.load File.expand_path('../syntax/lexer.citrus', __FILE__)
|
10
|
+
Citrus.load File.expand_path('../syntax/literals.citrus', __FILE__)
|
11
|
+
Citrus.load File.expand_path('../syntax/expressions.citrus', __FILE__)
|
30
12
|
Citrus.load File.expand_path('../syntax/finitio.citrus', __FILE__)
|
31
13
|
|
32
14
|
def self.parse(source, *args, &bl)
|
@@ -38,13 +20,16 @@ module Finitio
|
|
38
20
|
parse(source, root: "system").to_ast
|
39
21
|
end
|
40
22
|
|
41
|
-
def self.compile(source,
|
42
|
-
|
23
|
+
def self.compile(source, cpl = nil)
|
24
|
+
cpl = Compilation.coerce(cpl)
|
25
|
+
parse(source, root: "system").compile(cpl)
|
26
|
+
cpl.resolve_proxies!.system
|
43
27
|
end
|
44
28
|
|
45
|
-
def self.compile_type(source)
|
46
|
-
|
29
|
+
def self.compile_type(source, cpl = nil)
|
30
|
+
cpl = Compilation.coerce(cpl)
|
31
|
+
parse(source, root: "type").compile(cpl)
|
47
32
|
end
|
48
33
|
|
49
|
-
end
|
50
|
-
end
|
34
|
+
end # module Syntax
|
35
|
+
end # module Finitio
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
include Node
|
5
|
+
|
6
|
+
WORLD_VAR = "__world"
|
7
|
+
|
8
|
+
def self.included(by)
|
9
|
+
by.extend(Node::ClassHelpers)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_proc()
|
13
|
+
src = to_proc_src
|
14
|
+
::Kernel.eval(src)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_proc_src
|
18
|
+
initializer = free_variables
|
19
|
+
.map{|v| "#{v} = __world.fetch(:#{v})" }
|
20
|
+
.join("\n")
|
21
|
+
<<-SRC.gsub(/^[ ]{10}/m,'')
|
22
|
+
->(__world){
|
23
|
+
unless __world.is_a?(Hash) && __world.keys.all?{|k| k.is_a?(Symbol) }
|
24
|
+
raise "Invalid world: " + __world.inspect
|
25
|
+
end
|
26
|
+
#{initializer}
|
27
|
+
#{to_proc_source}
|
28
|
+
}
|
29
|
+
SRC
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_proc_source
|
33
|
+
raise NotImplementedError
|
34
|
+
end
|
35
|
+
|
36
|
+
def free_variables
|
37
|
+
[].tap{|fvs| _free_variables(fvs) }.uniq
|
38
|
+
end
|
39
|
+
|
40
|
+
def _free_variables(fvs)
|
41
|
+
raise NotImplementedError
|
42
|
+
end
|
43
|
+
|
44
|
+
end # module Expr
|
45
|
+
end # module Syntax
|
46
|
+
end # module Finitio
|
47
|
+
require_relative 'expr/logic_dyadic'
|
48
|
+
require_relative 'expr/logic_not'
|
49
|
+
require_relative 'expr/comparison'
|
50
|
+
require_relative 'expr/arith_op'
|
51
|
+
require_relative 'expr/unary_minus_op'
|
52
|
+
require_relative 'expr/oo_call'
|
53
|
+
require_relative 'expr/fn_call'
|
54
|
+
require_relative 'expr/identifier'
|
55
|
+
require_relative 'expr/literal'
|
56
|
+
require_relative 'expr/parenthesized'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module ArithOp
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :left, :op, :right
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
l, r = left.to_proc_source, right.to_proc_source
|
11
|
+
"#{l}.#{op.to_str.strip}(#{r})"
|
12
|
+
end
|
13
|
+
|
14
|
+
def _free_variables(fvs)
|
15
|
+
left._free_variables(fvs)
|
16
|
+
right._free_variables(fvs)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # module ArithOp
|
20
|
+
end # module Expr
|
21
|
+
end # module Syntax
|
22
|
+
end # module Finitio
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module Comparison
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :left, :op, :right
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
l, r = left.to_proc_source, right.to_proc_source
|
11
|
+
"#{l}.#{op.to_str.strip}(#{r})"
|
12
|
+
end
|
13
|
+
|
14
|
+
def _free_variables(fvs)
|
15
|
+
left._free_variables(fvs)
|
16
|
+
right._free_variables(fvs)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # module Comparison
|
20
|
+
end # module Expr
|
21
|
+
end # module Syntax
|
22
|
+
end # module Finitio
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module FnCall
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :fn
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
args = captures[:expr].map(&:to_proc_source)
|
11
|
+
first, rest = args.first, args[1..-1]
|
12
|
+
"#{first}.#{fn}(#{rest.join(',')})"
|
13
|
+
end
|
14
|
+
|
15
|
+
def _free_variables(fvs)
|
16
|
+
captures[:expr].each do |e|
|
17
|
+
e._free_variables(fvs)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end # module FnCall
|
22
|
+
end # module Expr
|
23
|
+
end # module Syntax
|
24
|
+
end # module Finitio
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module Identifier
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
def to_proc_source(free = true)
|
8
|
+
self.to_str
|
9
|
+
end
|
10
|
+
|
11
|
+
def _free_variables(fvs)
|
12
|
+
fvs << self.to_str
|
13
|
+
end
|
14
|
+
|
15
|
+
end # module Identifier
|
16
|
+
end # module Expr
|
17
|
+
end # module Syntax
|
18
|
+
end # module Finitio
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module Literal
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :literal
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
literal.to_ruby
|
11
|
+
end
|
12
|
+
|
13
|
+
def _free_variables(fvs)
|
14
|
+
end
|
15
|
+
|
16
|
+
end # module Identifier
|
17
|
+
end # module Expr
|
18
|
+
end # module Syntax
|
19
|
+
end # module Finitio
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module LogicDyadic
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :left, :op, :right
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
l, r = left.to_proc_source, right.to_proc_source
|
11
|
+
"(#{l} #{op} #{r})"
|
12
|
+
end
|
13
|
+
|
14
|
+
def _free_variables(fvs)
|
15
|
+
left._free_variables(fvs)
|
16
|
+
right._free_variables(fvs)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # module LogicDyadic
|
20
|
+
end # module Expr
|
21
|
+
end # module Syntax
|
22
|
+
end # module Finitio
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module LogicNot
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :term
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
l = term.to_proc_source
|
11
|
+
"!(#{l})"
|
12
|
+
end
|
13
|
+
|
14
|
+
def _free_variables(fvs)
|
15
|
+
term._free_variables(fvs)
|
16
|
+
end
|
17
|
+
|
18
|
+
end # module LogicNot
|
19
|
+
end # module Expr
|
20
|
+
end # module Syntax
|
21
|
+
end # module Finitio
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module OOCall
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :left
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
l = left.to_proc_source
|
11
|
+
r = captures[:right]
|
12
|
+
.map{|s| "fetch(:#{s.to_proc_source})" }
|
13
|
+
.join('.')
|
14
|
+
"#{l}.#{r}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def _free_variables(fvs)
|
18
|
+
left._free_variables(fvs)
|
19
|
+
end
|
20
|
+
|
21
|
+
end # module OOCall
|
22
|
+
end # module Expr
|
23
|
+
end # module Syntax
|
24
|
+
end # module Finitio
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module Parenthesized
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :expr
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
expr.to_proc_source
|
11
|
+
end
|
12
|
+
|
13
|
+
def _free_variables(fvs)
|
14
|
+
expr._free_variables(fvs)
|
15
|
+
end
|
16
|
+
|
17
|
+
end # module Parenthesized
|
18
|
+
end # module Expr
|
19
|
+
end # module Syntax
|
20
|
+
end # module Finitio
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Finitio
|
2
|
+
module Syntax
|
3
|
+
module Expr
|
4
|
+
module UnaryMinusOp
|
5
|
+
include Expr
|
6
|
+
|
7
|
+
capture :term
|
8
|
+
|
9
|
+
def to_proc_source
|
10
|
+
r = term.to_proc_source
|
11
|
+
"-#{r}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def _free_variables(fvs)
|
15
|
+
term._free_variables(fvs)
|
16
|
+
end
|
17
|
+
|
18
|
+
end # module UnaryMinusOp
|
19
|
+
end # module Expr
|
20
|
+
end # module Syntax
|
21
|
+
end # module Finitio
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'finitio/syntax/lexer'
|
2
|
+
require 'finitio/syntax/literals'
|
3
|
+
|
4
|
+
grammar Finitio::Syntax::Expressions
|
5
|
+
include Finitio::Syntax::Lexer
|
6
|
+
include Finitio::Syntax::Literals
|
7
|
+
|
8
|
+
rule expr
|
9
|
+
logic_or_expr
|
10
|
+
end
|
11
|
+
|
12
|
+
### Boolean logic
|
13
|
+
|
14
|
+
rule logic_or_expr
|
15
|
+
(left:logic_and_expr op:or_operator right:logic_or_expr)
|
16
|
+
<Finitio::Syntax::Expr::LogicDyadic>
|
17
|
+
| logic_and_expr
|
18
|
+
end
|
19
|
+
|
20
|
+
rule or_operator
|
21
|
+
spaces? ('or' | '||') spaces?
|
22
|
+
end
|
23
|
+
|
24
|
+
rule logic_and_expr
|
25
|
+
(left:logic_not_expr op:and_operator right:logic_and_expr)
|
26
|
+
<Finitio::Syntax::Expr::LogicDyadic>
|
27
|
+
| logic_not_expr
|
28
|
+
end
|
29
|
+
|
30
|
+
rule and_operator
|
31
|
+
spaces? ('and' | '&&') spaces?
|
32
|
+
end
|
33
|
+
|
34
|
+
rule logic_not_expr
|
35
|
+
(not_operator term:logic_term)
|
36
|
+
<Finitio::Syntax::Expr::LogicNot>
|
37
|
+
| logic_term
|
38
|
+
end
|
39
|
+
|
40
|
+
rule not_operator
|
41
|
+
spaces? ('!' | 'not') spaces?
|
42
|
+
end
|
43
|
+
|
44
|
+
rule logic_term
|
45
|
+
comparison_expr
|
46
|
+
end
|
47
|
+
|
48
|
+
### Comparisons
|
49
|
+
|
50
|
+
rule comparison_expr
|
51
|
+
(left:arith_expr op:comparison_operator right:comparison_expr)
|
52
|
+
<Finitio::Syntax::Expr::Comparison>
|
53
|
+
| arith_expr
|
54
|
+
end
|
55
|
+
|
56
|
+
rule comparison_operator
|
57
|
+
spaces? ('==' | '!=' | '<=' | '>=' | '<' | '>' | '=~') spaces?
|
58
|
+
end
|
59
|
+
|
60
|
+
### Arithmetic
|
61
|
+
|
62
|
+
rule arith_expr
|
63
|
+
arith_plusminus_expr
|
64
|
+
end
|
65
|
+
|
66
|
+
rule arith_plusminus_expr
|
67
|
+
(left:arith_multdiv_expr op:plusminus_operator right:arith_plusminus_expr)
|
68
|
+
<Finitio::Syntax::Expr::ArithOp>
|
69
|
+
| arith_multdiv_expr
|
70
|
+
end
|
71
|
+
|
72
|
+
rule plusminus_operator
|
73
|
+
spaces? ('+' | '-') spaces?
|
74
|
+
end
|
75
|
+
|
76
|
+
rule arith_multdiv_expr
|
77
|
+
(left:arith_term_expr op:multdiv_operator right:arith_multdiv_expr)
|
78
|
+
<Finitio::Syntax::Expr::ArithOp>
|
79
|
+
| arith_term_expr
|
80
|
+
end
|
81
|
+
|
82
|
+
rule multdiv_operator
|
83
|
+
spaces? ('*' | '/') spaces?
|
84
|
+
end
|
85
|
+
|
86
|
+
rule arith_term_expr
|
87
|
+
('-' term:arith_term_expr)
|
88
|
+
<Finitio::Syntax::Expr::UnaryMinusOp>
|
89
|
+
| oo_call
|
90
|
+
end
|
91
|
+
|
92
|
+
### Function calls
|
93
|
+
|
94
|
+
rule oo_call
|
95
|
+
(left:fn_call ('.' right:identifier_expr)+)
|
96
|
+
<Finitio::Syntax::Expr::OOCall>
|
97
|
+
| fn_call
|
98
|
+
end
|
99
|
+
|
100
|
+
rule fn_call
|
101
|
+
(fn:term_expr '(' spaces? expr? (comma expr)* spaces? ')')
|
102
|
+
<Finitio::Syntax::Expr::FnCall>
|
103
|
+
| term_expr
|
104
|
+
end
|
105
|
+
|
106
|
+
###
|
107
|
+
|
108
|
+
rule term_expr
|
109
|
+
identifier_expr
|
110
|
+
| literal_expr
|
111
|
+
| parenthesized_expr
|
112
|
+
end
|
113
|
+
|
114
|
+
rule identifier_expr
|
115
|
+
(!keyword /[a-zA-Z_][a-zA-Z0-9_]*/)
|
116
|
+
<Finitio::Syntax::Expr::Identifier>
|
117
|
+
end
|
118
|
+
|
119
|
+
rule literal_expr
|
120
|
+
(!eof literal)
|
121
|
+
<Finitio::Syntax::Expr::Literal>
|
122
|
+
end
|
123
|
+
|
124
|
+
rule parenthesized_expr
|
125
|
+
('(' spaces? expr spaces? ')')
|
126
|
+
<Finitio::Syntax::Expr::Parenthesized>
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|