Skip to content

Commit

Permalink
Implement str builtin in Ruby, JavaScript, and stackmac backends.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Jun 28, 2021
1 parent 9332418 commit dccf806
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ AST nodes should have source line numbers, it would be really nice.

C backend. Other backends (Python? Java? CIL? Scheme?)

Runtime support for `str` in Ruby backend and JavaScript backend.

### Design ###

Convenience:
Expand Down
1 change: 1 addition & 0 deletions src/castile/backends/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def compile(self, ast):
var len = function(s) { return s.length; };
var concat = function(s1,s2) { return s1 + s2; };
var substr = function(s,p,k) { return s.substr(p, k); };
var str = function(n) { return '' + n; };
var repr = function(o) {
if (typeof o === "string") {
Expand Down
4 changes: 4 additions & 0 deletions src/castile/backends/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def compile(self, ast):
s[p..p+(k-1)]
}
str = lambda { |n|
n.to_s
}
def repr o
if o == true
return "True"
Expand Down
6 changes: 6 additions & 0 deletions src/castile/stackmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def run(program, strings):
s = strings[stack.pop()]
strings.append(builtin(s, p, k))
stack.append(len(strings) - 1)
elif name == 'str':
n = stack.pop()
strings.append(builtin(n))
stack.append(len(strings) - 1)
else:
raise NotImplementedError(name)
elif op == 'rts':
Expand Down Expand Up @@ -233,4 +237,6 @@ def main(args):
arg = int(arg)
p.append((op, arg))

if debug:
print(strings)
run(p, strings)

0 comments on commit dccf806

Please sign in to comment.