Soml parser (outdated, not used)
Go to file
Torsten Ruger 6302038914 make 1.9 save 2015-09-15 20:04:32 +03:00
lib make 1.9 save 2015-09-15 20:04:32 +03:00
test using sat gem 2015-09-15 12:27:54 +03:00
.gitignore merged the origin 2014-06-04 19:49:40 +03:00
.travis.yml add cc token 2015-07-18 13:10:00 +03:00
Gemfile fix gemfile 2015-09-15 19:46:05 +03:00
Gemfile.lock fix gemfile 2015-09-15 19:46:05 +03:00
LICENSE Initial commit 2014-06-04 19:27:53 +03:00
README.md update read me 2015-08-28 23:36:48 +03:00
Rakefile using raketasks 2015-05-03 22:58:33 +03:00
salama-reader.gemspec change to citrus wip 2015-08-27 21:01:12 +03:00

README.md

Build Status Gem Version Code Climate Test Coverage

Salama Reader

The parser part of salama is now a standalone gem. It parses ruby using Citrus and no other dependencies.

This is interesting if you want to generate executable code, like salama, but also for other things, like code analysis.

Also it is very educational, as it is very readable code, and not too much of it.

Parser

The main parser per se is in parser/salama , but it just pulls in all the parts.

All the other files are ruby modules representing aspects of the parser. Most names are quite self explanatory, but here is a list:

  • basic_type defines just that. Strings, symbols, integers, floats , also comments and space
  • call_site is a function call. May be qualified, but currently must still have braches
  • compound types are hash and array definitions. Hashes still need curlies
  • control is if statement which still must have an else
  • expression is a helper for all code allowed in a function
  • function definition must have braces too
  • keywords is just a list of them
  • operator expression are binary operators (see also below). There's a surprising amount
  • return statement are straightforward
  • while still needs a do, though i think in ruby a newline is sufficient

Ast

The Abtract Syntax Tree (ast) layer puts the parsed code into objects, so they are nice and easy to work with.

The Classes don't really define any functionality, that is done in Salama, or can be done in any code using this. Salama just adds a compile function to each class, but a visitor pattern would do just as well.

The functionality that is in there is mainly to do with testing. Equality is defined, but also inspect in such a way that it's output (which you get from a failing test) can be pasted straight into the test case as the expected result.

Citrus

Citrus is really great in that it:

  • does not generate code but instead gives a syntax to define a grammar
  • uses ruby modules so one can split the grammars up

Todo

A random list of things left for the future

  • extract commonality of function call/definition,array, hash and multi assignment comma lists
  • break and next
  • blocks
  • more loops, i'm not even sure what ruby supports
  • ifs without else, also elsif
  • negative tests

Operators

See the operators.rb for details. Below is a list from the web of how it should be.

Operator list from http://stackoverflow.com/questions/21060234/ruby-operator-precedence-table

N A M Operator(s) Description


1 R Y ! ~ + boolean NOT, bitwise complement, unary plus (unary plus may be redefined from Ruby 1.9 with +@)

2 R Y ** exponentiation 1 R Y - unary minus (redefine with -@)

2 L Y * / % multiplication, division, modulo (remainder) 2 L Y + - addition (or concatenation), subtraction

2 L Y << >> bitwise shift-left (or append), bitwise shift-right 2 L Y & bitwise AND

2 L Y | ^ bitwise OR, bitwise XOR (exclusive OR) 2 L Y < <= >= > ordering

2 N Y == === != =~ !~ <=> equality, pattern matching, comparison (!= and !~ may not be redefined prior to Ruby 1.9)

2 L N && boolean AND 2 L N || boolean OR

2 N N .. ... range creation (inclusive and exclusive) and boolean flip-flops

3 R N ? : ternary if-then-else (conditional) 2 L N rescue exception-handling modifier

2 R N = assignment 2 R N **= *= /= %= += -= assignment 2 R N <<= >>= assignment 2 R N &&= &= ||= |= ^= assignment

1 N N defined? test variable definition and type 1 R N not boolean NOT (low precedence) 2 L N and or boolean AND, boolean OR (low precedence) 2 N N if unless while until conditional and loop modifiers