Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Bargnesi committed Aug 7, 2015
2 parents 31eb68b + 6d59c63 commit 95d2784
Show file tree
Hide file tree
Showing 58 changed files with 763 additions and 617 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ tmp
.ruby-version
Gemfile.lock
*.gem
*.so
*.bundle
*.swp
/tags
*.tags
Makefile
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require 'rake/clean'
FileList['tasks/**/*.rake'].each { |task| import task }

if RUBY_PLATFORM =~ /java/
# JRuby will not compile libbel. Native extensions are not supported.
task :default => [:unit, :integration]
else
task :default => [:compile, :unit, :integration]
Expand Down
5 changes: 3 additions & 2 deletions bel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Gem::Specification.new do |spec|
'[email protected]'
]
spec.files = [
Dir.glob("lib/**/*.rb"),
Dir.glob("ext/**/*.{c,h,def}"),
Dir.glob('lib/**/*.rb'),
Dir.glob('ext/**/*.{c,h,def}'),
Dir.glob('lib/bel/libbel/ext/{java,mingw}/**/*.{so,bundle}'),
__FILE__,
'LICENSE',
'INSTALL.md',
Expand Down
4 changes: 2 additions & 2 deletions ext/mri/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if RUBY_PLATFORM =~ /java/
if RUBY_PLATFORM =~ /java/i

# Don't do anything when run in JRuby; this allows gem installation to pass.
# We need to write a dummy Makefile so that RubyGems doesn't think compilation
Expand All @@ -18,5 +18,5 @@
# CC=gcc-4.8 gem install bel.rb
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']

create_makefile('bel/libbel')
create_makefile('bel/libbel/ext/libbel')
end
8 changes: 3 additions & 5 deletions lib/bel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

require_relative 'bel/script'

unless RUBY_PLATFORM =~ /java/
require_relative 'bel/libbel.rb'
require_relative 'bel/parser'
require_relative 'bel/completion'
end
require_relative 'bel/libbel.rb'
require_relative 'bel/parser'
require_relative 'bel/completion'

include BEL::Language
include BEL::Namespace
Expand Down
318 changes: 141 additions & 177 deletions lib/bel/libbel.rb
Original file line number Diff line number Diff line change
@@ -1,200 +1,164 @@
module LibBEL
require_relative 'libbel/library_load_error'
require_relative 'libbel/library_resolver'

class << self
module BEL
module LibBEL

def rubinius?
defined?(RUBY_ENGINE) && ("rbx" == RUBY_ENGINE)
end
extend LibraryResolver

# @api_private
# Determines the correct extension path for your ruby version
def extension_path(ruby_version = RUBY_VERSION)
version =
case ruby_version
when /^2\.0/
'2.0'
when /^2\.1/
'2.1'
when /^2\.2/
'2.2'
else
fail RuntimeError.new("Do not support Ruby #{RUBY_VERSION}.")
end
class << self

File.join(File.expand_path(File.dirname(__FILE__)), version)
end
def rubinius?
defined?(RUBY_ENGINE) && ("rbx" == RUBY_ENGINE)
end

# @api_private
# Determine FFI constant for this ruby engine.
def find_ffi
if rubinius?
# Use ffi gem instead of rubinius-bundled FFI
# Rubinius 2.5.2 does not seem to support FFI::ManagedStruct
require "ffi"
::FFI
else # mri, jruby, etc
require "ffi"
::FFI
# @api_private
# Determine FFI constant for this ruby engine.
def find_ffi
if rubinius?
# Use ffi gem instead of rubinius-bundled FFI
# Rubinius 2.5.2 does not seem to support FFI::ManagedStruct
require "ffi"
::FFI
else # mri, jruby, etc
require "ffi"
::FFI
end
end
end

# @api_private
# Extend with the correct ffi implementation.
def load_ffi
ffi_module = LibBEL::find_ffi
extend ffi_module::Library
ffi_module
end
# @api_private
# Extend with the correct ffi implementation.
def load_ffi
ffi_module = BEL::LibBEL::find_ffi
extend ffi_module::Library
ffi_module
end

# @api_private
# Loads the libbel shared library.
def load_libBEL
ffi_module = find_ffi
extend ffi_module::Library

lib_path = resolve_library('libbel')

# @api_private
# Loads the libbel shared library.
def load_libBEL
ffi_module = find_ffi
extend ffi_module::Library

# libbel.so: Linux and MinGW
# libbel.bundle: Mac OSX
messages = []
library_loaded = ['libbel.so', 'libbel.bundle'].map { |library_file|
[
library_file,
File.join(
File.expand_path(File.dirname(__FILE__)),
library_file
)
]
}.any? do |library_file, library_path|
# Try loading top-level shared library.
begin
ffi_lib library_path
ffi_lib lib_path
true
rescue LoadError => exception1
# Try loading version-specific shared library.
begin
ffi_lib File.join(extension_path, library_file)
true
rescue LoadError => exception2
messages << exception1.to_s
messages << exception2.to_s
false
end
rescue LoadError => err
raise LibraryLoadError.new('libbel', err)
false
end
end

if !library_loaded
msg = messages.map { |msg| " #{msg}" }.join("\n")
fail LoadError.new("Failed to load libbel library. Details:\n#{msg}")
end
end
end

# Constant holding the FFI module for this ruby engine.
FFI = LibBEL::load_ffi
LibBEL::load_libBEL

# typedef enum bel_token_type
enum :bel_token_type, [
:IDENT, 0,
:STRING,
:O_PAREN,
:C_PAREN,
:COLON,
:COMMA,
:SPACES
]

enum :bel_ast_node_type, [
:BEL_TOKEN, 0,
:BEL_VALUE
]

enum :bel_ast_token_type, [
:BEL_TOKEN_STATEMENT, 0,
:BEL_TOKEN_SUBJECT,
:BEL_TOKEN_OBJECT,
:BEL_TOKEN_REL,
:BEL_TOKEN_TERM,
:BEL_TOKEN_ARG,
:BEL_TOKEN_NV
]

enum :bel_ast_value_type, [
:BEL_VALUE_FX, 0,
:BEL_VALUE_REL,
:BEL_VALUE_PFX,
:BEL_VALUE_VAL
]

require_relative 'libbel/bel_ast_structs'
require_relative 'libbel/bel_token'
require_relative 'libbel/bel_token_iterator'
require_relative 'libbel/bel_token_list'

# bel ast
attach_function :bel_ast_as_string, [:pointer ], :string
attach_function :bel_copy_ast_node, [:pointer ], :pointer
attach_function :bel_free_ast, [:pointer ], :void
attach_function :bel_free_ast_node, [:pointer ], :void
attach_function :bel_new_ast, [ ], :pointer
attach_function :bel_new_ast_node_token, [:bel_ast_token_type ], :pointer
attach_function :bel_new_ast_node_value, [:bel_ast_value_type, :string ], :pointer
attach_function :bel_print_ast, [:pointer ], :void
attach_function :bel_print_ast_node, [:pointer, :string ], :void
attach_function :bel_set_value, [:pointer, :string ], :pointer

# bel token
attach_function :bel_new_token, [:bel_token_type, :pointer, :pointer, :pointer], :pointer
attach_function :bel_new_token_iterator, [:pointer ], :pointer
attach_function :bel_new_token_list, [:int ], :pointer
attach_function :bel_print_token, [:pointer ], :void
attach_function :bel_print_token_list, [:pointer ], :void
attach_function :bel_token_iterator_end, [:pointer ], :int
attach_function :bel_token_iterator_get, [:pointer ], :pointer
attach_function :bel_token_iterator_next, [:pointer ], :void
attach_function :free_bel_token, [:pointer ], :void
attach_function :free_bel_token_iterator, [:pointer ], :void
attach_function :free_bel_token_list, [:pointer ], :void

# tokenize
attach_function :bel_tokenize_term, [:string ], :pointer

# parse
attach_function :bel_parse_statement, [:string ], :pointer
attach_function :bel_parse_term, [:string ], :pointer

def self.parse_statement(bel_string)
LibBEL::BelAst.new(LibBEL::bel_parse_statement(bel_string))
end
# Constant holding the FFI module for this ruby engine.
FFI = BEL::LibBEL::load_ffi
BEL::LibBEL::load_libBEL

# typedef enum bel_token_type
enum :bel_token_type, [
:IDENT, 0,
:STRING,
:O_PAREN,
:C_PAREN,
:COLON,
:COMMA,
:SPACES
]

enum :bel_ast_node_type, [
:BEL_TOKEN, 0,
:BEL_VALUE
]

enum :bel_ast_token_type, [
:BEL_TOKEN_STATEMENT, 0,
:BEL_TOKEN_SUBJECT,
:BEL_TOKEN_OBJECT,
:BEL_TOKEN_REL,
:BEL_TOKEN_TERM,
:BEL_TOKEN_ARG,
:BEL_TOKEN_NV
]

enum :bel_ast_value_type, [
:BEL_VALUE_FX, 0,
:BEL_VALUE_REL,
:BEL_VALUE_PFX,
:BEL_VALUE_VAL
]

require_relative 'libbel/bel_ast_structs'
require_relative 'libbel/bel_token'
require_relative 'libbel/bel_token_iterator'
require_relative 'libbel/bel_token_list'

# bel ast
attach_function :bel_ast_as_string, [:pointer ], :string
attach_function :bel_copy_ast_node, [:pointer ], :pointer
attach_function :bel_free_ast, [:pointer ], :void
attach_function :bel_free_ast_node, [:pointer ], :void
attach_function :bel_new_ast, [ ], :pointer
attach_function :bel_new_ast_node_token, [:bel_ast_token_type ], :pointer
attach_function :bel_new_ast_node_value, [:bel_ast_value_type, :string ], :pointer
attach_function :bel_print_ast, [:pointer ], :void
attach_function :bel_print_ast_node, [:pointer, :string ], :void
attach_function :bel_set_value, [:pointer, :string ], :pointer

# bel token
attach_function :bel_new_token, [:bel_token_type, :pointer, :pointer, :pointer], :pointer
attach_function :bel_new_token_iterator, [:pointer ], :pointer
attach_function :bel_new_token_list, [:int ], :pointer
attach_function :bel_print_token, [:pointer ], :void
attach_function :bel_print_token_list, [:pointer ], :void
attach_function :bel_token_iterator_end, [:pointer ], :int
attach_function :bel_token_iterator_get, [:pointer ], :pointer
attach_function :bel_token_iterator_next, [:pointer ], :void
attach_function :free_bel_token, [:pointer ], :void
attach_function :free_bel_token_iterator, [:pointer ], :void
attach_function :free_bel_token_list, [:pointer ], :void

# tokenize
attach_function :bel_tokenize_term, [:string ], :pointer

# parse
attach_function :bel_parse_statement, [:string ], :pointer
attach_function :bel_parse_term, [:string ], :pointer

def self.parse_statement(bel_string)
BEL::LibBEL::BelAst.new(BEL::LibBEL::bel_parse_statement(bel_string))
end

def self.parse_term(bel_string)
LibBEL::BelAst.new(LibBEL::bel_parse_term(bel_string))
end
def self.parse_term(bel_string)
BEL::LibBEL::BelAst.new(BEL::LibBEL::bel_parse_term(bel_string))
end

def self.tokenize_term(bel_string)
LibBEL::BelTokenList.new(self.bel_tokenize_term(bel_string))
end
def self.tokenize_term(bel_string)
BEL::LibBEL::BelTokenList.new(self.bel_tokenize_term(bel_string))
end

def self.copy_ast(bel_ast)
copy_root = self.copy_ast_node(bel_ast.root)
new_ast = LibBEL::BelAst.new(LibBEL::bel_new_ast())
new_ast.root = copy_root
new_ast
end
def self.copy_ast(bel_ast)
copy_root = self.copy_ast_node(bel_ast.root)
new_ast = BEL::LibBEL::BelAst.new(BEL::LibBEL::bel_new_ast())
new_ast.root = copy_root
new_ast
end

def self.copy_ast_node(bel_ast_node)
LibBEL::BelAstNode.new(LibBEL::bel_copy_ast_node(bel_ast_node))
end
def self.copy_ast_node(bel_ast_node)
BEL::LibBEL::BelAstNode.new(BEL::LibBEL::bel_copy_ast_node(bel_ast_node))
end

def self.print_ast(bel_ast)
LibBEL::bel_print_ast(bel_ast)
end
def self.print_ast(bel_ast)
BEL::LibBEL::bel_print_ast(bel_ast)
end

def self.print_token(token)
self.bel_print_token(token.pointer)
end
def self.print_token(token)
self.bel_print_token(token.pointer)
end

def self.print_token_list(list)
self.bel_print_token_list(list.pointer)
def self.print_token_list(list)
self.bel_print_token_list(list.pointer)
end
end
end

Loading

0 comments on commit 95d2784

Please sign in to comment.