Coder Social home page Coder Social logo

goruby's Introduction

goruby

GoRuby, an implementation of Ruby written in Go

Build Status GoDoc Test Coverage Maintainability Go Report Card

Contribution

If anyone wants to help to get the project to the real implementation please ping me or fork it and send a pull request.

Community

License

This project is released under the terms of the MIT License, as found in the LICENSE file.

REPL

There is a basic REPL within cmd/girb. It supports multiline expressions and all syntax elements the language supports yet.

To run it ad hoc run go run cmd/girb/main.go and exit the REPL with CTRL-D.

Command

To run the command as one off run go run main.go.

Supported features

goruby Command

  • parse program files
  • program file arguments
  • Flags
    • -0[octal] specify record separator (\0, if no argument)
    • -a autosplit mode with -n or -p (splits $_ into $F)
    • -c check syntax only
    • -Cdirectory cd to directory before executing your script
    • -d set debugging flags (set $DEBUG to true)
    • -e 'command' one line of script. Several -e's allowed. Omit [programfile]
    • -Eex[:in] specify the default external and internal character encodings
    • -Fpattern split() pattern for autosplit (-a)
    • -i[extension] edit ARGV files in place (make backup if extension supplied)
    • -Idirectory specify $LOAD_PATH directory (may be used more than once)
    • -l enable line ending processing
    • -n assume 'while gets(); ... end' loop around your script
    • -p assume loop like -n but print line also like sed
    • -rlibrary require the library before executing your script
    • -s enable some switch parsing for switches after script name
    • -S look for the script using PATH environment variable
    • -T[level=1] turn on tainting checks
    • -v print version number, then turn on verbose mode
    • -w turn warnings on for your script
    • -W[level=2] set warning level; 0=silence, 1=medium, 2=verbose
    • -x[directory] strip off text before #!ruby line and perhaps cd to directory
    • -h show this message, --help for more info

girb Command

  • parse program files
  • program file arguments
  • Flags
    • -f Suppress read of ~/.irbrc
    • -m Bc mode (load mathn, fraction or matrix are available)
    • -d Set $DEBUG to true (same as `ruby -d')
    • -r load-module Same as `ruby -r'
    • -I path Specify $LOAD_PATH directory
    • -U Same as ruby -U
    • -E enc Same as ruby -E
    • -w Same as ruby -w
    • -W[level=2] Same as ruby -W
    • --context-mode n Set n[0-3] to method to create Binding Object, when new workspace was created
    • --echo Show result(default)
    • --noecho Don't show result
    • --inspect Use `inspect' for output (default except for bc mode)
    • --noinspect Don't use inspect for output
    • --readline Use Readline extension module
    • --noreadline Don't use Readline extension module
    • --prompt prompt-mode/--prompt-mode prompt-mode Switch prompt mode. Pre-defined prompt modes are default', simple', xmp' and inf-ruby'
    • --inf-ruby-mode Use prompt appropriate for inf-ruby-mode on emacs. Suppresses --readline.
    • --sample-book-mode/--simple-prompt Simple prompt mode
    • --noprompt No prompt mode
    • --single-irb Share self with sub-irb.
    • --tracer Display trace for each execution of commands.
    • --back-trace-limit n Display backtrace top n and tail n. The default value is 16.
    • --irb_debug n Set internal debug level to n (not for popular use)
    • --verbose Show details
    • --noverbose Don't show details
    • -v, --version Print the version of irb
    • -h, --help Print help
    • -- Separate options of irb from the list of command-line args

Supported language feature

  • everything is an object
    • allow method calls on everything
    • operators are method calls
  • full UTF8 support
    • Unicode identifier
    • Unicode symbols
  • functions
    • with parens
    • without parens
    • return keyword
    • default values for parameters
    • keyword arguments
    • block arguments
    • hash as last argument without braces
  • function calls
    • with parens
    • without parens
    • with block arguments
  • conditionals
    • if
    • if/else
    • if/elif/else
    • tenary ? :
    • unless
    • unless/else
    • case
    • ||
    • &&
  • control flow
    • for loop
    • while loop
    • until loop
    • break
    • next
    • redo
    • flip flop
  • numbers
    • integers
      • integer arithmetics
      • integers 1234
      • integers with underscores 1_234
      • decimal numbers 0d170, 0D170
      • octal numbers 0252, 0o252, 0O252
      • hexadecimal numbers 0xaa, 0xAa, 0xAA, 0Xaa, 0XAa, 0XaA
      • binary numbers 0b10101010, 0B10101010
    • floats
      • float arithmetics
      • 12.34
      • 1234e-2
      • 1.234E1
      • floats with underscores 2.2_22
  • booleans
  • strings
    • double quoted
    • single quoted
    • character literals (?\n, ?a,...)
    • %q{}
    • %Q{}
    • heredoc
      • without indentation (<<EOF)
      • indented (<<-EOF)
      • “squiggly” heredoc <<~
      • quoted heredoc
        • single quotes <<-'HEREDOC'
        • double quotes <<-"HEREDOC"
        • backticks <<-`HEREDOC`"
    • escaped characters
      • \a bell, ASCII 07h (BEL)
      • \b backspace, ASCII 08h (BS)
      • \t horizontal tab, ASCII 09h (TAB)
      • \n newline (line feed), ASCII 0Ah (LF)
      • \v vertical tab, ASCII 0Bh (VT)
      • \f form feed, ASCII 0Ch (FF)
      • \r carriage return, ASCII 0Dh (CR)
      • \e escape, ASCII 1Bh (ESC)
      • \s space, ASCII 20h (SPC)
      • \\ backslash, \
      • \nnn octal bit pattern, where nnn is 1-3 octal digits ([0-7])
      • \xnn hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
      • \unnnn Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
      • \u{nnnn ...} Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
      • \cx or \C-x control character, where x is an ASCII printable character
      • \M-x meta character, where x is an ASCII printable character
      • \M-\C-x meta control character, where x is an ASCII printable character
      • \M-\cx same as above
      • \c\M-x same as above
      • \c? or \C-? delete, ASCII 7Fh (DEL)
    • interpolation #{}
    • automatic concatenation
  • arrays
    • array literal [1,2]
    • array indexing arr[2]
    • splat
    • array decomposition
    • implicit array assignment
    • array of strings %w{}
    • array of symbols %i{}
  • nil
  • hashes
    • literal with => notation
    • literal with key: notation
    • indexing hash[:foo]
    • every Ruby Object can be a hash key
  • symbols
    • :symbol
    • :"symbol"
    • :"symbol" with interpolation
    • :'symbol'
    • %s{symbol}
    • singleton symbols
  • regexp
    • /regex/
    • %r{regex}
  • ranges
    • .. inclusive
    • ... exclusive
  • procs ->
  • variables
    • variable assignments
    • globals
  • operators
    • +
    • -
    • /
    • *
    • !
    • <
    • >
    • ** (pow)
    • % (modulus)
    • & (AND)
    • ^ (XOR)
    • >> (right shift)
    • << (left shift, append)
    • == (equal)
    • != (not equal)
    • === (case equality)
    • =~ (pattern match)
    • !~ (does not match)
    • <=> (comparison or spaceship operator)
    • <= (less or equal)
    • >= (greater or equal)
    • assignment operators
      • +=
      • -=
      • /=
      • *=
      • %=
      • **=
      • &=
      • |=
      • ^=
      • <<=
      • >>=
      • ||=
      • &&=
  • function blocks (procs)
  • error handling
    • begin/rescue
    • ensure
    • retry
  • constants
  • scope operator ::
  • classes
    • class objects
    • class Class
    • instance variables
    • class variables
    • class methods
    • instance methods
    • method overrides
    • private
    • protected
    • public
    • inheritance
    • constructors
    • new
    • self
    • singleton classes (also known as the metaclass or eigenclass) class << self
    • assigment methods
    • self defined classes
    • self defined classes with inheritance
  • modules
  • object main
  • comments '#'

goruby's People

Contributors

alex-slynko avatar apotheon avatar mitch000001 avatar nicolai86 avatar skx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

goruby's Issues

Is this project still active?

What version of Go are you using (go version)?

1.11

What operating system and processor architecture are you using (go env)?

linux, amd64.

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

ee5b7274d84c124a2bd3b3d6f1e2c78d31148cc5

In short I wondered if you're still working upon this project? There are a couple of things I see in the TODO-list of the README file that I'd be happy to contribute. But it might be that you've stalled, or wish to proceed under your own pace.

As an example it seems like supporting '\t', '\n', etc should be a simple matter of updating lexString in lexer/lexer.go. I'd be happy to do that, but it might be you have a different approach you might prefer.

License?

Hi, thanks for this awesome project. I'm considering to use RubyGo's lexer and parser for one of my projects and I couldn't find a license file which shows the license details.

Could use some context in the readme

Hello! :) It would be awesome to see some context about the project in the readme.

Who is this for? What is the goal? Is this for learning or eventually for some production use? What are the a caveats and tradeoffs?

Thoughts?

no prefix parse function for type NEWLINE found

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go 1.10.3

What operating system and processor architecture are you using (go env)?

linux / amd64

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

HEAD

What did you do?

go run goruby/main.go -e "$(printf "d w{}.o,\n w{}.o")" (note the use of bash's printf to insert a newline into the expression)

What did you expect to see?

something different than what I did see :)

What did you see instead?

syntax error, Parsing errors:
        1:8: no prefix parse function for type NEWLINE found

exit status 1

Failing expression derived from something like this (part of a rakefile):

dll :dllname,
     cdefine('DLLNAME_EXPORTS'),
     installer { uuid('blah') }.only_if(:cond),
     installer { uuid('blaa') }.only_if(:cond2),
     otherstuff

Doesn't work at all except for -e

goruby anythingthatdoesntexists.rb --> no error

goruby anythingthatexists.rb --> no output
goruby ./anythingthatexists.rb --> no output
goruby pwd/anythingthatexists.rb --> no output
cat anythingthatexists.rb | goruby --> no output

gourby -v=true --> no output

goruby --version --> at last.. an output:

flag provided but not defined: -version
Usage of goruby:
  -e="": one line of script. Several -e's allowed. Omit [programfile]
  -v=false: print version number, then turn on verbose mode

goruby -e="puts 1+1" --> gives 2
`

Aliasing

Any plans to implement aliasing?

`$:` missing

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/401208/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/401208"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9q/wtg2gqwd57x88fy6qw79klsn6zl9w9/T/go-build841597827=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

git show -s --format=%H
# => 07b0c0a9b40cc6551c20cc830965e315ab61c803

What did you do?

go run main.go -e 'puts $:.nil?'

What did you expect to see?

false

What did you see instead?

`File.expand_path` undefined

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/401208/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/401208"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9q/wtg2gqwd57x88fy6qw79klsn6zl9w9/T/go-build854393630=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

git show -s --format=%H
# => 07b0c0a9b40cc6551c20cc830965e315ab61c803

What did you do?

touch /tmp/foo
go run main.go -e 'puts File.expand_path('..', '/tmp')

What did you expect to see?

/

What did you see instead?


uninitialized constant File
exit status 1

parsing errors: expected identifier, got ast.ExpressionList

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

What operating system and processor architecture are you using (go env)?

linux / amd64

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

ee5b727 (which is HEAD)

What did you do?

I want goruby to produce an AST I can walk, but it's encountering errors with files rake accepts.

go run main.go -e "d w{}.o, w{}.o"

What did you expect to see?

If it parsed, I'd expect complaints of undefined variables or methods, such as

undefined local variable or method `p' for main:Object

What did you see instead?

syntax error, Parsing errors:
        could not parse call expression: expected identifier, got token 'ast.ExpressionList'
        1:12: no prefix parse function for type . found

exit status 1

When I hit the error in issue #53, I didn't see an easy fix, so I started pre-processing the ruby to remove newlines following commas. That led me to this error.

Segment fault after module definition when using functions like `puts`

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.1 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/mitch/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mitch"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/65/106pnkdj4q158c3xbx9h6ksr0000gn/T/go-build213543851=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

master (ec0bb1d)

What did you do?

$ module_segment_fault=<<EOF
module Foo
end

puts 'hello'
EOF
$ go run main.go -e "${module_segment_fault}"

What did you expect to see?

hello

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x10bc60d]

goroutine 1 [running]:
github.com/goruby/goruby/object.Send(0x117c540, 0xc420086500, 0xc420082590, 0x4, 0xc4200864f0, 0x1, 0x1, 0x1, 0x0, 0x0, ...)
	/Users/mitch/src/github.com/goruby/goruby/object/send.go:6 +0x5d
github.com/goruby/goruby/evaluator.Eval(0x123a0c8, 0xc4200980a0, 0x117d760, 0xc4200824e0, 0x0, 0x0, 0x0, 0x0)
	/Users/mitch/src/github.com/goruby/goruby/evaluator/evaluator.go:104 +0xe33
github.com/goruby/goruby/evaluator.Eval(0x123a068, 0xc42007e750, 0x117d760, 0xc4200824e0, 0x0, 0x0, 0x0, 0x0)
	/Users/mitch/src/github.com/goruby/goruby/evaluator/evaluator.go:26 +0x117d
github.com/goruby/goruby/evaluator.evalProgram(0xc4200825c0, 0x2, 0x2, 0x117d760, 0xc4200824e0, 0xc420082580, 0x0, 0x1d, 0x20)
	/Users/mitch/src/github.com/goruby/goruby/evaluator/evaluator.go:147 +0x95
github.com/goruby/goruby/evaluator.Eval(0x117ba40, 0xc4200825a0, 0x117d760, 0xc4200824e0, 0x0, 0x0, 0x0, 0x0)
	/Users/mitch/src/github.com/goruby/goruby/evaluator/evaluator.go:24 +0x278
github.com/goruby/goruby/interpreter.(*interpreter).Interpret(0xc420086260, 0xc420082580, 0x1d, 0x21d, 0xc420082580, 0x1d, 0x0)
	/Users/mitch/src/github.com/goruby/goruby/interpreter/interpreter.go:32 +0xc3
main.main()
	/Users/mitch/src/github.com/goruby/goruby/main.go:53 +0x220
exit status 2

`__FILE__` undefined

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/401208/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/401208"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9q/wtg2gqwd57x88fy6qw79klsn6zl9w9/T/go-build854393630=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What version of GoRuby are you using (git show -s --format=%H or goruby -version)?

git show -s --format=%H
# => 07b0c0a9b40cc6551c20cc830965e315ab61c803

What did you do?

cat <<EOF > /tmp/foo
> puts __FILE__
> EOF
go run main.go /tmp/foo

What did you expect to see?

/tmp/foo

What did you see instead?

undefined local variable or method `__FILE__' for main:Object
exit status 1

Allow chaining of method calls

At the moment it is not possible to chain method calls.
If one would want to send methods to a class after retrieving it via obj.class she has to save the result in a variable to do something with it.

E.g.:
1.class.methods leads to a parsing error.

why

Dont take this the wrong way, but i am curious what led you to do this ?

I have been in situations where i needed similar interpretted languages so that end users have a scripting language to extend a system, so maybe thats the reasons too for you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.