serpent 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e064fffc2cbbd7c57f6a9f8c8b0a4cf326ec73b
4
- data.tar.gz: d7403a3c974afdac5933e4c6d960fdc642c9c25e
3
+ metadata.gz: 811c4565b51dd15e5eeb48a256059514f0c0b533
4
+ data.tar.gz: c1beae6806e7f1b2b814e25faa7ea844d63db767
5
5
  SHA512:
6
- metadata.gz: 676765556e178545eac61b86590d55e9b6885897cbbb959df28b8e6a4c1c376665e164491c256e0ac856049df8c3d1e9fbdb605d8e18875bdd7a68bf296673fa
7
- data.tar.gz: e5c93ddca6f6dcd41979f7aa4e0a4e6731292df4665d2b477548d3cdfe63df26517fd38caa8cbb201a5cf17aa1d88be649371ae909e6bac5b916fa113d8d2ffa
6
+ metadata.gz: b03d4526f59ee065ea878e6e6e7abfb2d68c90a9e3bdf34e887683247e4d272104375da2b2173749f73aedbcee063e6a650358fd7740237d8dca8d459607d6cd
7
+ data.tar.gz: c1a76b77781c5ec3fdfc5d978dcc71b47895387250310c80f885ba8d9162fc737899ca5d9eafef82b04c37bf8a2ee44c96e0b3d7c83634fe079ebcfdc929a9d4
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
1
  tags
2
2
  tmp
3
- lib/serpent/serpent.so
3
+ lib/serpent/serpent_ffi.so
4
4
  .bundle
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- serpent (0.1.0)
4
+ serpent (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://ruby.taobao.org/
data/README.md CHANGED
@@ -22,6 +22,9 @@ cd ..
22
22
 
23
23
  # compile extension
24
24
  rake compile
25
+
26
+ # run test
27
+ rake
25
28
  ```
26
29
 
27
30
  ## Usage
data/Rakefile CHANGED
@@ -3,7 +3,8 @@ require 'rake/testtask'
3
3
 
4
4
  require 'fileutils'
5
5
 
6
- Rake::ExtensionTask.new "serpent" do |ext|
6
+ Rake::ExtensionTask.new "serpent_ffi" do |ext|
7
+ ext.ext_dir = 'ext/serpent'
7
8
  ext.lib_dir = 'lib/serpent'
8
9
  end
9
10
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : ascii-8bit -*-
1
2
  require 'mkmf-rice'
2
3
 
3
4
  #include_dir = File.expand_path('../../../include', __FILE__)
@@ -7,4 +8,4 @@ require 'mkmf-rice'
7
8
 
8
9
  abort "missing libserpent.so" unless have_library "serpent"
9
10
 
10
- create_makefile('serpent/serpent')
11
+ create_makefile('serpent/serpent_ffi')
@@ -0,0 +1,74 @@
1
+ #include "rice/Module.hpp"
2
+ #include "rice/Array.hpp"
3
+ using namespace Rice;
4
+
5
+ #include "libserpent/parser.h"
6
+ #include "libserpent/funcs.h"
7
+
8
+ Array serialize_metadata(Metadata m) {
9
+ Array a;
10
+ a.push(m.file);
11
+ a.push(m.ln);
12
+ a.push(m.ch);
13
+ return a;
14
+ }
15
+
16
+ Array serialize_node(Node n) {
17
+ Array a;
18
+ a.push(n.type);
19
+ a.push(n.val);
20
+ a.push(serialize_metadata(n.metadata));
21
+ for(unsigned i = 0; i < n.args.size(); i++)
22
+ a.push(serialize_node(n.args[i]));
23
+ return a;
24
+ }
25
+
26
+ /*
27
+ * Serpent functions
28
+ */
29
+
30
+ std::string serpent_compile(Object self, std::string code) {
31
+ return compile(code);
32
+ }
33
+
34
+ //std::string serpent_compile_lll(Object self, Node program) {
35
+ //return compileLLL(program);
36
+ //}
37
+
38
+ Array serpent_compile_to_lll(Object self, std::string code) {
39
+ return serialize_node(compileToLLL(code));
40
+ }
41
+
42
+ Array serpent_parse_lll(Object self, std::string code) {
43
+ return serialize_node(parseLLL(code));
44
+ }
45
+
46
+ std::string serpent_mk_signature(Object self, std::string input) {
47
+ return mkSignature(input);
48
+ }
49
+
50
+ std::string serpent_mk_full_signature(Object self, std::string input) {
51
+ return mkFullSignature(input);
52
+ }
53
+
54
+ std::string serpent_mk_contract_info(Object self, std::string input) {
55
+ return mkContractInfoDecl(input);
56
+ }
57
+
58
+ unsigned int serpent_get_prefix(Object self, std::string signature) {
59
+ return getPrefix(signature);
60
+ }
61
+
62
+ extern "C"
63
+ void Init_serpent_ffi() {
64
+ Module rb_mSerpent = define_module("Serpent");
65
+
66
+ Module rb_mSerpentFFI = define_module_under(rb_mSerpent, "FFI")
67
+ .define_module_function("compile", &serpent_compile)
68
+ .define_module_function("compile_to_lll", &serpent_compile_to_lll)
69
+ .define_module_function("parse_lll", &serpent_parse_lll)
70
+ .define_module_function("mk_signature", &serpent_mk_signature)
71
+ .define_module_function("mk_full_signature", &serpent_mk_full_signature)
72
+ .define_module_function("mk_contract_info", &serpent_mk_contract_info)
73
+ .define_module_function("get_prefix", &serpent_get_prefix);
74
+ }
@@ -1,20 +1,38 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  require 'json'
2
4
 
3
5
  require 'serpent/version'
4
- require 'serpent/serpent'
6
+ require 'serpent/serpent_ffi'
7
+ require 'serpent/node'
8
+ require 'serpent/caller'
5
9
 
6
10
  module Serpent
7
- class <<self
8
- def mk_signature(s)
9
- JSON.parse mk_raw_signature(s)
10
- end
11
-
12
- def mk_full_signature(s)
13
- JSON.parse mk_raw_full_signature(s)
14
- end
15
-
16
- def mk_contract_info(s)
17
- JSON.parse mk_raw_contract_info(s)
18
- end
11
+
12
+ extend self
13
+
14
+ def compile(code, **kwargs)
15
+ Caller.new.compile code, **kwargs
16
+ end
17
+
18
+ def compile_to_lll(code, **kwargs)
19
+ Caller.new.compile_to_lll code, **kwargs
20
+ end
21
+
22
+ def get_prefix(signature)
23
+ FFI.get_prefix signature
19
24
  end
25
+
26
+ def mk_signature(s)
27
+ JSON.parse FFI.mk_signature(s)
28
+ end
29
+
30
+ def mk_full_signature(s)
31
+ JSON.parse FFI.mk_full_signature(s)
32
+ end
33
+
34
+ def mk_contract_info(s)
35
+ JSON.parse FFI.mk_contract_info(s)
36
+ end
37
+
20
38
  end
@@ -0,0 +1,39 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
3
+ module Serpent
4
+ class Caller
5
+
6
+ def compile(code, **kwargs)
7
+ FFI.compile pre_transform(code, kwargs)
8
+ end
9
+
10
+ def compile_to_lll(code, **kwargs)
11
+ Node.build FFI.compile_to_lll(pre_transform(code, kwargs))
12
+ end
13
+
14
+ def parse_to_lll(code)
15
+ FFI.parse_lll(code)
16
+ end
17
+
18
+ private
19
+
20
+ def take(x)
21
+ x.instance_of?(String) ? FFI.parse_lll(x) : x.out
22
+ end
23
+
24
+ def pre_transform(code, params)
25
+ code2 = ''
26
+ params.each do |k, v|
27
+ v = %{"#{v}"} if v.instance_of?(String)
28
+ code2 += "macro $#{k}:\n #{v}\n"
29
+ end
30
+
31
+ if File.exist?(code)
32
+ code2 + "inset('#{code}')"
33
+ else
34
+ code2 + code
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,80 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
3
+ module Serpent
4
+ module Node
5
+
6
+ class Metadata
7
+ def initialize(ary)
8
+ @file = ary[0]
9
+ @ln = ary[1]
10
+ @ch = ary[2]
11
+ end
12
+
13
+ def out
14
+ [@file, @ln, @ch]
15
+ end
16
+ end
17
+
18
+ class Token
19
+ def initialize(val, metadata)
20
+ @val = val
21
+ @metadata = metadata
22
+ end
23
+
24
+ def out
25
+ [0, @val, @metadata.out]
26
+ end
27
+
28
+ def to_s
29
+ @val.to_s
30
+ end
31
+ alias :inspect :to_s
32
+ end
33
+
34
+ class Astnode
35
+ def initialize(val, args, metadata)
36
+ @val = val
37
+ @args = args.map {|arg| Node.build arg }
38
+ @metadata = Metadata.new(metadata)
39
+ end
40
+
41
+ def out
42
+ [1, @val, @metadata.out] + @args.map(&:out)
43
+ end
44
+
45
+ def to_s
46
+ o = "(#{@val}"
47
+ subs = @args.map(&:to_s)
48
+
49
+ k = 0
50
+ out = " "
51
+ while k < subs.size && o != "(seq"
52
+ break if subs[k].include?("\n") || (out + subs[k]).size >= 80
53
+ out += subs[k] + " "
54
+ k += 1
55
+ end
56
+
57
+ if k < subs.size
58
+ o += out + "\n "
59
+ o += subs[k..-1].join("\n").split("\n").join("\n ")
60
+ o += "\n)"
61
+ else
62
+ o += out[0...-1] + ')'
63
+ end
64
+
65
+ o
66
+ end
67
+ alias :inspect :to_s
68
+ end
69
+
70
+ def self.build(ary)
71
+ if ary[0] == 0
72
+ Token.new ary[1], ary[2]
73
+ else
74
+ args = ary.size > 3 ? ary[3..-1] : []
75
+ Astnode.new ary[1], args, ary[2]
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : ascii-8bit -*-
1
2
  module Serpent
2
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
3
4
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : ascii-8bit -*-
1
2
  $:.unshift File.expand_path('../../lib', __FILE__)
2
3
 
3
4
  require 'minitest/autorun'
@@ -11,7 +12,15 @@ def main(a,b):
11
12
  EOF
12
13
 
13
14
  def test_compile
14
- assert_equal decode_hex('604a80600b6000396055567c0100000000000000000000000000000000000000000000000000000000600035046397d857aa8114156048576004356040526024356060526060516040510a60805260206080f35b505b6000f3'), Serpent.compile(CODE)
15
+ assert_equal decode_hex('604a80600b6000396055567c0100000000000000000000000000000000000000000000000000000000600035046397d857aa8114156048576004356040526024356060526060516040510a60805260206080f35b505b6000f3'), Serpent.compile(CODE, foo: 1)
16
+ end
17
+
18
+ def test_compile_to_lll
19
+ assert_match /^\(return 0 \n/, Serpent.compile_to_lll(CODE).to_s
20
+ end
21
+
22
+ def test_parse_lll
23
+ assert_equal [0, 'def', ['main', 0, 0]], Serpent::FFI.parse_lll(CODE)
15
24
  end
16
25
 
17
26
  def test_mk_full_signature
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serpent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Xie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-11 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -96,8 +96,10 @@ files:
96
96
  - README.md
97
97
  - Rakefile
98
98
  - ext/serpent/extconf.rb
99
- - ext/serpent/serpent.cpp
99
+ - ext/serpent/serpent_ffi.cpp
100
100
  - lib/serpent.rb
101
+ - lib/serpent/caller.rb
102
+ - lib/serpent/node.rb
101
103
  - lib/serpent/version.rb
102
104
  - serpent.gemspec
103
105
  - test/serpent_test.rb
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  version: '0'
122
124
  requirements: []
123
125
  rubyforge_project:
124
- rubygems_version: 2.4.5
126
+ rubygems_version: 2.5.1
125
127
  signing_key:
126
128
  specification_version: 4
127
129
  summary: Ruby binding to Ethereum Serpent compiler.
@@ -1,34 +0,0 @@
1
- #include "rice/Module.hpp"
2
- using namespace Rice;
3
-
4
- #include "libserpent/funcs.h"
5
-
6
- std::string serpent_compile(Object self, std::string code) {
7
- return compile(code);
8
- }
9
-
10
- std::string serpent_mk_raw_signature(Object self, std::string input) {
11
- return mkSignature(input);
12
- }
13
-
14
- std::string serpent_mk_raw_full_signature(Object self, std::string input) {
15
- return mkFullSignature(input);
16
- }
17
-
18
- std::string serpent_mk_raw_contract_info(Object self, std::string input) {
19
- return mkContractInfoDecl(input);
20
- }
21
-
22
- unsigned int serpent_get_prefix(Object self, std::string signature) {
23
- return getPrefix(signature);
24
- }
25
-
26
- extern "C"
27
- void Init_serpent() {
28
- Module rb_mSerpent = define_module("Serpent")
29
- .define_module_function("compile", &serpent_compile)
30
- .define_module_function("mk_raw_signature", &serpent_mk_raw_signature)
31
- .define_module_function("mk_raw_full_signature", &serpent_mk_raw_full_signature)
32
- .define_module_function("mk_raw_contract_info", &serpent_mk_raw_contract_info)
33
- .define_module_function("get_prefix", &serpent_get_prefix);
34
- }