p-lang 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ígor Bonadio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = p-lang
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Ígor Bonadio. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "p-lang"
8
+ gem.summary = %Q{P programming language}
9
+ gem.description = %Q{P programming language}
10
+ gem.email = "igorbonadio@gmail.com"
11
+ gem.homepage = "http://github.com/igorbonadio/p-lang"
12
+ gem.authors = ["Igor Bonadio"]
13
+ gem.add_development_dependency "treetop", ">= 0"
14
+ gem.add_development_dependency "shoulda", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "p-lang #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :patch: 1
3
+ :build:
4
+ :major: 0
5
+ :minor: 0
data/bin/p-lang ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), '/../lib/p-lang')
4
+
5
+ if ARGV[0]
6
+ @parser = PLangParser.new
7
+
8
+ ast = @parser.parse(File.readlines(ARGV[0]).join("")).build.collect(&:to_sexp)
9
+
10
+ vm = PLang::VM.new(ast)
11
+
12
+ vm.execute!
13
+ end
data/lib/p-lang.rb ADDED
@@ -0,0 +1,24 @@
1
+ ROOT_PATH = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'treetop'
5
+
6
+ Treetop.load File.join(ROOT_PATH, '/parser/p-lang')
7
+
8
+ require File.join(ROOT_PATH, '/parser/nodes')
9
+ require File.join(ROOT_PATH, '/parser/ast')
10
+
11
+ require File.join(ROOT_PATH, '/vm/environment')
12
+ require File.join(ROOT_PATH, '/vm/vm')
13
+ require File.join(ROOT_PATH, '/vm/proc')
14
+ require File.join(ROOT_PATH, '/vm/pobject')
15
+
16
+ require File.join(ROOT_PATH, '/vm/std/io')
17
+
18
+ module PLang
19
+ class VM
20
+ def initialize_global_environment(env)
21
+ IO.def_pfunctions(env)
22
+ end
23
+ end
24
+ end
data/lib/parser/ast.rb ADDED
@@ -0,0 +1,168 @@
1
+ module PLang
2
+ module Ast
3
+ class PStatement
4
+ def initialize(expression)
5
+ @expression = expression
6
+ end
7
+
8
+ def to_sexp
9
+ @expression.to_sexp
10
+ end
11
+ end
12
+
13
+ class PUnOp
14
+ attr_reader :op
15
+ attr_reader :right
16
+ def initialize(op, right)
17
+ @op = op
18
+ @right = right
19
+ end
20
+
21
+ def to_sexp
22
+ [@op, @right.to_sexp]
23
+ end
24
+ end
25
+
26
+ class PBinOp
27
+ attr_reader :op
28
+ attr_reader :left
29
+ attr_reader :right
30
+
31
+ def initialize(op, left, right)
32
+ @op = op.to_sym
33
+ @left = left
34
+ @right = right
35
+ end
36
+
37
+ def to_sexp
38
+ [@op, @left.to_sexp, @right.to_sexp]
39
+ end
40
+ end
41
+
42
+ class PLiteral
43
+ attr_reader :type
44
+ attr_reader :value
45
+
46
+ def initialize(type, value)
47
+ @type = type.to_sym
48
+ @value = value
49
+ end
50
+
51
+ def to_sexp
52
+ [:literal, @type, @value]
53
+ end
54
+ end
55
+
56
+ class PId
57
+ attr_reader :name
58
+
59
+ def initialize(name)
60
+ @name = name.to_sym
61
+ end
62
+
63
+ def to_sexp
64
+ [:id, @name]
65
+ end
66
+ end
67
+
68
+ class PObject
69
+ attr_reader :name
70
+
71
+ def initialize(name, params)
72
+ @name = name.to_sym
73
+ @params = params
74
+ end
75
+
76
+ def to_sexp
77
+ [:object, @name, @params.collect(&:to_sexp)]
78
+ end
79
+ end
80
+
81
+ class PCall
82
+ attr_reader :cid
83
+
84
+ def initialize(cid, params)
85
+ @cid = cid
86
+ @params = params
87
+ end
88
+
89
+ def to_sexp
90
+ @cid = @cid.to_sexp
91
+ if @cid == [:id, :if]
92
+ @params = @params.collect(&:to_sexp)
93
+ if @params.size == 3
94
+ [:if, @params[0], @params[1], @params[2]]
95
+ else
96
+ raise "'if' error"
97
+ end
98
+ elsif @cid == [:id, :begin]
99
+ [:begin, @params.collect(&:to_sexp)]
100
+ else
101
+ [:call, @cid, @params.collect(&:to_sexp)]
102
+ end
103
+ end
104
+ end
105
+
106
+ class PLet
107
+ attr_reader :var
108
+ attr_reader :val
109
+
110
+ def initialize(var, val)
111
+ @var = var
112
+ @val = val
113
+ end
114
+
115
+ def to_sexp
116
+ [:let, @var.to_sexp, @val.to_sexp]
117
+ end
118
+ end
119
+
120
+ class PLambda
121
+ attr_reader :params
122
+ attr_reader :expr
123
+ attr_reader :where
124
+ attr_accessor :next_lambda
125
+
126
+ def initialize(params, expr, where)
127
+ @params = params
128
+ @expr = expr
129
+ @where = where
130
+ @next_lambda = nil
131
+ end
132
+
133
+ def to_sexp
134
+ [:lambda, @params.collect(&:to_sexp), @expr.to_sexp, @where.collect(&:to_sexp), @next_lambda]
135
+ end
136
+ end
137
+
138
+ class PObjectCall
139
+ attr_reader :obj
140
+ attr_reader :msg
141
+
142
+ def initialize(obj, msg)
143
+ @obj = obj
144
+ @msg = msg
145
+ end
146
+
147
+ def to_sexp
148
+ [:object_call, @obj.to_sexp, @msg.to_sexp]
149
+ end
150
+ end
151
+
152
+ class PObjectLet
153
+ attr_reader :obj
154
+ attr_reader :id
155
+ attr_reader :value
156
+
157
+ def initialize(obj, id, value)
158
+ @obj = obj
159
+ @id = id
160
+ @value = value
161
+ end
162
+
163
+ def to_sexp
164
+ [:object_let, @obj.to_sexp, @id.to_sexp, @value.to_sexp]
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,212 @@
1
+ module PLang
2
+
3
+ module NStatements
4
+ def build
5
+ elements.collect { |element| PLang::Ast::PStatement.new(element.statement.build) }
6
+ end
7
+ end
8
+
9
+ module NCStatementList
10
+ def build
11
+ stm = statement_list.build
12
+ unless stm.class == Array
13
+ stm = [stm]
14
+ end
15
+ [statement.build] + stm
16
+ end
17
+ end
18
+
19
+ module NBinOp
20
+ def build
21
+ PLang::Ast::PBinOp.new(op.text_value, expr.build, statement.build)
22
+ end
23
+ end
24
+
25
+ module NUnOp
26
+ def build
27
+ PLang::Ast::PUnOp.new(:not, statement.build)
28
+ end
29
+ end
30
+
31
+ module NInteger
32
+ def build
33
+ PLang::Ast::PLiteral.new(:integer, text_value.to_i)
34
+ end
35
+ end
36
+
37
+ module NDecimal
38
+ def build
39
+ PLang::Ast::PLiteral.new(:decimal, text_value.to_f)
40
+ end
41
+ end
42
+
43
+ module NString
44
+ def build
45
+ PLang::Ast::PLiteral.new(:string, str.text_value)
46
+ end
47
+ end
48
+
49
+ module NChar
50
+ def build
51
+ PLang::Ast::PLiteral.new(:char, c.text_value)
52
+ end
53
+ end
54
+
55
+ module NId
56
+ def build
57
+ PLang::Ast::PId.new(text_value)
58
+ end
59
+ end
60
+
61
+ module NObject
62
+ def build
63
+ if obj_list.respond_to?(:build)
64
+ params = obj_list.build
65
+ unless params.class == Array
66
+ params = [params]
67
+ end
68
+ PLang::Ast::PObject.new(id.text_value, params)
69
+ else
70
+ PLang::Ast::PObject.new(id.text_value, [])
71
+ end
72
+ end
73
+ end
74
+
75
+ module NObjectList
76
+ def build
77
+ statement_list.build
78
+ end
79
+ end
80
+
81
+ module NParemExpr
82
+ def build
83
+ statement.build
84
+ end
85
+ end
86
+
87
+ module NCall
88
+ def build
89
+ params = []
90
+ if cparams.respond_to?(:build)
91
+ params = cparams.build
92
+ unless params.class == Array
93
+ params = [params]
94
+ end
95
+ end
96
+ PLang::Ast::PCall.new(cid.build, params)
97
+ end
98
+ end
99
+
100
+ module NVarLet
101
+ def build
102
+ PLang::Ast::PLet.new(var.build, statement.build)
103
+ end
104
+ end
105
+
106
+ module NLambda
107
+ def build
108
+ @where = []
109
+ if where.respond_to?(:build)
110
+ @where = where.build
111
+ end
112
+ @params = []
113
+ if params.respond_to?(:build)
114
+ @params = params.build
115
+ end
116
+ PLang::Ast::PLambda.new(@params, statement.build, @where)
117
+ end
118
+ end
119
+
120
+ module NCLambda
121
+ def build
122
+ nlambda = lamb.build
123
+ nlambda.next_lambda = lambda.build.to_sexp
124
+ nlambda
125
+ end
126
+ end
127
+
128
+ module NWhere
129
+ def build
130
+ w = where_params.build
131
+ if w.class == Array
132
+ w
133
+ else
134
+ [w]
135
+ end
136
+ end
137
+ end
138
+
139
+ module NCWhereParams
140
+ def build
141
+ w = where_params.build
142
+ unless w.class == Array
143
+ w = [w]
144
+ end
145
+ [let.build] + w
146
+ end
147
+ end
148
+
149
+ module NLambdaParams
150
+ def build
151
+ params = lambda_params_list.build
152
+ if params.class == Array
153
+ params
154
+ else
155
+ [params]
156
+ end
157
+ end
158
+ end
159
+
160
+ module NCLambdaParamsList
161
+ def build
162
+ params = lambda_params_list.build
163
+ unless params.class == Array
164
+ params = [params]
165
+ end
166
+ [form.build] + params
167
+ end
168
+ end
169
+
170
+ module NObjectForm
171
+ def build
172
+ if obj_form_list.respond_to?(:build)
173
+ PLang::Ast::PObject.new(id.text_value, obj_form_list.build)
174
+ else
175
+ PLang::Ast::PObject.new(id.text_value, [])
176
+ end
177
+ end
178
+ end
179
+
180
+ module NObjectGet
181
+ def build
182
+ PLang::Ast::PObjectCall.new(expr.build, id.build)
183
+ end
184
+ end
185
+
186
+ module NObjectMsg
187
+ def build
188
+ params = []
189
+ if statement_list.respond_to?(:build)
190
+ params = statement_list.build
191
+ unless params.class == Array
192
+ params = [params]
193
+ end
194
+ end
195
+ PLang::Ast::PCall.new(PLang::Ast::PObjectCall.new(expr.build, id.build), [expr.build] | params)
196
+ end
197
+ end
198
+
199
+ module NObjectLet
200
+ def build
201
+ PLang::Ast::PObjectLet.new(object_form.build, var.build, statement.build)
202
+ end
203
+ end
204
+
205
+ module NBoolean
206
+ def build
207
+ PLang::Ast::PLiteral.new(:boolean, text_value.to_sym)
208
+ end
209
+ end
210
+
211
+ end
212
+