Skip to content

Commit

Permalink
Minimal name-mangling in Ruby backend. Demo "Parse, don't validate".
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Jun 30, 2021
1 parent 02474a7 commit 0183881
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
99 changes: 71 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1123,34 +1123,77 @@ you can in actuality create recursive, but finite, data structures.

You may want to use helper functions to hide this ugliness.

/| struct list {
/| value: string;
/| next: list|void;
/| }
/| fun singleton(v: string) {
/| make list(value:v, next:null as list|void)
/| }
/| fun cons(v: string, l: list) {
/| make list(value:v, next:l as list|void)
/| }
/| fun nth(n, l: list) {
/| u = l as list|void;
/| v = u;
/| k = n;
/| while k > 1 {
/| typecase u is void { break; }
/| typecase u is list { v = u.next; }
/| u = v;
/| k = k - 1;
/| }
/| return u
/| }
/| main = fun() {
/| l = cons("first", singleton("second"));
/| g = nth(2, l);
/| typecase g is list { print(g.value); }
/| }
/= second
| struct list {
| value: string;
| next: list|void;
| }
|
| fun empty() {
| return null as list|void
| }
|
| fun cons(v: string, l: list|void) {
| make list(value:v, next:l) as list|void
| }
|
| fun nth(n, l: list|void) {
| u = l;
| v = u;
| k = n;
| while k > 1 {
| typecase u is void { break; }
| typecase u is list { v = u.next; }
| u = v;
| k = k - 1;
| }
| return u
| }
|
| main = fun() {
| l = cons("first", cons("second", cons("third", empty())));
| h = nth(2, l);
| typecase h is list { print(h.value); }
| }
= second

And in fact, you can restrict the union types to smaller sets to
better indicate the allowable types of the functions. For example,
`cons` always returns a list, so that should be its return type,
not `list|void`. Likewise, `nth` requires a list. In this way we
can implement some of the "Parse, don't Validate" approach.

| struct list {
| value: string;
| next: list|void;
| }
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
| }
|
| fun singleton(v: string) {
| make list(value:v, next:null as list|void)
| }
|
| fun nth(n, l: list) {
| u = l as list|void;
| v = u;
| k = n;
| while k > 1 {
| typecase u is void { break; }
| typecase u is list { v = u.next; }
| u = v;
| k = k - 1;
| }
| return u
| }
|
| main = fun() {
| l = cons("first", cons("second", singleton("third")));
| h = nth(2, l);
| typecase h is list { print(h.value); }
| }
= second

Structs may be empty.

Expand Down
9 changes: 7 additions & 2 deletions src/castile/backends/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def write_indent(self, x):
self.out.write(' ' * self.indent)
self.out.write(x)

def mangle(self, ident):
if ident.startswith('next'):
return '{}_'.format(ident)
return ident

def compile(self, ast):
if ast.tag == 'Program':
self.write(PRELUDE)
Expand Down Expand Up @@ -117,7 +122,7 @@ def compile(self, ast):
for child in ast.children:
self.compile(child)
elif ast.tag == 'VarDecl':
self.write_indent('%s = nil;\n' % ast.value)
self.write_indent('%s = nil;\n' % self.mangle(ast.value))
elif ast.tag == 'Block':
for child in ast.children:
self.compile(child)
Expand All @@ -137,7 +142,7 @@ def compile(self, ast):
self.compile(ast.children[1])
self.write(')')
elif ast.tag == 'VarRef':
self.write(ast.value)
self.write(self.mangle(ast.value))
elif ast.tag == 'FunCall':
self.compile(ast.children[0])
self.write('.call(')
Expand Down

0 comments on commit 0183881

Please sign in to comment.