nscript 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/nscript +5 -0
- data/examples/code.ns +173 -0
- data/examples/helloworld.ns +6 -0
- data/lib/nscript/command_line.rb +235 -0
- data/lib/nscript/lexer/lexer.rb +230 -0
- data/lib/nscript/lexer/rewriter.rb +238 -0
- data/lib/nscript/parser/grammar.y +483 -0
- data/lib/nscript/parser/nodes.rb +932 -0
- data/lib/nscript/parser/parse_error.rb +22 -0
- data/lib/nscript/parser/parser.rb +2662 -0
- data/lib/nscript/scope.rb +74 -0
- data/lib/nscript/value.rb +62 -0
- data/lib/nscript/version.rb +3 -0
- data/lib/nscript.rb +15 -0
- data/nscript.gemspec +30 -0
- metadata +115 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
module NScript
|
2
|
+
|
3
|
+
class Scope
|
4
|
+
|
5
|
+
attr_reader :parent, :expressions, :function, :variables, :temp_variable
|
6
|
+
|
7
|
+
def initialize(parent, expressions, function)
|
8
|
+
@parent, @expressions, @function = parent, expressions, function
|
9
|
+
@variables = {}
|
10
|
+
@temp_variable = @parent ? @parent.temp_variable.dup : '__a'
|
11
|
+
end
|
12
|
+
|
13
|
+
def find(name, remote=false)
|
14
|
+
found = check(name)
|
15
|
+
return found if found || remote
|
16
|
+
@variables[name.to_sym] = :var
|
17
|
+
found
|
18
|
+
end
|
19
|
+
|
20
|
+
def parameter(name)
|
21
|
+
@variables[name.to_sym] = :param
|
22
|
+
end
|
23
|
+
|
24
|
+
def check(name)
|
25
|
+
return true if @variables[name.to_sym]
|
26
|
+
!!(@parent && @parent.check(name))
|
27
|
+
end
|
28
|
+
|
29
|
+
def reset(name)
|
30
|
+
@variables[name.to_sym] = false
|
31
|
+
end
|
32
|
+
|
33
|
+
def free_variable
|
34
|
+
@temp_variable.succ! while check(@temp_variable)
|
35
|
+
@variables[@temp_variable.to_sym] = :var
|
36
|
+
Value.new(@temp_variable.dup)
|
37
|
+
end
|
38
|
+
|
39
|
+
def assign(name, value, top=false)
|
40
|
+
return @parent.assign(name, value, top) if top && @parent
|
41
|
+
@variables[name.to_sym] = Value.new(value)
|
42
|
+
end
|
43
|
+
|
44
|
+
def declarations?(body)
|
45
|
+
!declared_variables.empty? && body == @expressions
|
46
|
+
end
|
47
|
+
|
48
|
+
def assignments?(body)
|
49
|
+
!assigned_variables.empty? && body == @expressions
|
50
|
+
end
|
51
|
+
|
52
|
+
def declared_variables
|
53
|
+
@variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort
|
54
|
+
end
|
55
|
+
|
56
|
+
def assigned_variables
|
57
|
+
@variables.select {|k, v| v.is_a?(Value) }.sort_by {|pair| pair[0].to_s }
|
58
|
+
end
|
59
|
+
|
60
|
+
def compiled_declarations
|
61
|
+
declared_variables.join(', ')
|
62
|
+
end
|
63
|
+
|
64
|
+
def compiled_assignments
|
65
|
+
assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ')
|
66
|
+
end
|
67
|
+
|
68
|
+
def inspect
|
69
|
+
"<Scope:#{__id__} #{@variables.inspect}>"
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module NScript
|
2
|
+
|
3
|
+
class Value
|
4
|
+
|
5
|
+
attr_reader :value, :line
|
6
|
+
|
7
|
+
def initialize(value, line=nil)
|
8
|
+
@value, @line = value, line
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_str
|
12
|
+
@value.to_s
|
13
|
+
end
|
14
|
+
alias_method :to_s, :to_str
|
15
|
+
|
16
|
+
def to_sym
|
17
|
+
to_str.to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
def compile(o={})
|
21
|
+
to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def inspect
|
25
|
+
@value.inspect
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(other)
|
29
|
+
@value == other
|
30
|
+
end
|
31
|
+
|
32
|
+
def [](index)
|
33
|
+
@value[index]
|
34
|
+
end
|
35
|
+
|
36
|
+
def eql?(other)
|
37
|
+
@value.eql?(other)
|
38
|
+
end
|
39
|
+
|
40
|
+
def hash
|
41
|
+
@value.hash
|
42
|
+
end
|
43
|
+
|
44
|
+
def match(regex)
|
45
|
+
@value.match(regex)
|
46
|
+
end
|
47
|
+
|
48
|
+
def children
|
49
|
+
[]
|
50
|
+
end
|
51
|
+
|
52
|
+
def statement_only?
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def contains?
|
57
|
+
false
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/lib/nscript.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "nscript/version"
|
2
|
+
require_relative "nscript/lexer/lexer"
|
3
|
+
require_relative "nscript/parser/parser"
|
4
|
+
require_relative "nscript/parser/nodes"
|
5
|
+
require_relative "nscript/value"
|
6
|
+
require_relative "nscript/scope"
|
7
|
+
require_relative "nscript/lexer/rewriter"
|
8
|
+
require_relative "nscript/parser/parse_error"
|
9
|
+
|
10
|
+
module NScript
|
11
|
+
def self.compile(script, options={})
|
12
|
+
script = script.read if script.respond_to?(:read)
|
13
|
+
Parser.new.parse(script).compile(options)
|
14
|
+
end
|
15
|
+
end
|
data/nscript.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nscript/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nscript"
|
8
|
+
spec.version = NScript::VERSION
|
9
|
+
spec.authors = ["Storm Burpee", "Jared Bowers"]
|
10
|
+
spec.email = ["storm@appflow.com.au", "jared@appflow.com.au"]
|
11
|
+
|
12
|
+
spec.summary = "The NScript Compiler"
|
13
|
+
spec.homepage = "https://nscript.io"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.description = <<-END
|
16
|
+
NScript is a little language that compiles into JavaScript.
|
17
|
+
NScript is a faster, quicker way to write performant JS code.
|
18
|
+
END
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.executables = ["nscript"]
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nscript
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Storm Burpee
|
8
|
+
- Jared Bowers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-07-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.16'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.16'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
description: |
|
57
|
+
NScript is a little language that compiles into JavaScript.
|
58
|
+
NScript is a faster, quicker way to write performant JS code.
|
59
|
+
email:
|
60
|
+
- storm@appflow.com.au
|
61
|
+
- jared@appflow.com.au
|
62
|
+
executables:
|
63
|
+
- nscript
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- ".gitignore"
|
68
|
+
- ".rspec"
|
69
|
+
- ".travis.yml"
|
70
|
+
- CODE_OF_CONDUCT.md
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/nscript
|
77
|
+
- examples/code.ns
|
78
|
+
- examples/helloworld.ns
|
79
|
+
- lib/nscript.rb
|
80
|
+
- lib/nscript/command_line.rb
|
81
|
+
- lib/nscript/lexer/lexer.rb
|
82
|
+
- lib/nscript/lexer/rewriter.rb
|
83
|
+
- lib/nscript/parser/grammar.y
|
84
|
+
- lib/nscript/parser/nodes.rb
|
85
|
+
- lib/nscript/parser/parse_error.rb
|
86
|
+
- lib/nscript/parser/parser.rb
|
87
|
+
- lib/nscript/scope.rb
|
88
|
+
- lib/nscript/value.rb
|
89
|
+
- lib/nscript/version.rb
|
90
|
+
- nscript.gemspec
|
91
|
+
homepage: https://nscript.io
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.7.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: The NScript Compiler
|
115
|
+
test_files: []
|