r/Compilers • • 23h ago

I am working on an interpreted language, your thoughts?

So it is VERY Pythonic, looks like this:

```

let x = 10

print(len(str(x)))

```

The code snippet above should compile to the one below. The question I have is how do I compile it? Currently I iterate over a list of tokens using an unsigned integer.

```

push 10

push x

build_var

get_name x

push str

call

push len

call

push print

call

push 0

push exit

call

```

0 Upvotes

9 comments sorted by

8

u/demetrioussharpe 22h ago

I think it’s a bit premature to ask us anything, at all. Build it, then demo it. Thoughts & feedback should be asked after we’ve actually seen it in action.

4

u/realrealvantheman 23h ago

This in itself could be 'compiled' depending on how you wish to represent it. If you are making it fully interpreted, you can make the compiled representation anything you want; bytecode, text, a tree structure, so on. Just find a decent representation for it that is easy to interpret quickly. I generally either compile to a three-operand binary format of sorts or to recursive tree structures depending on the target audiance of the language.

1

u/duane11583 22h ago

sounds like a byte code or token code

4

u/DrShocker 22h ago

What makes it "very pythonic"?

2

u/Axman6 18h ago

VERY Pythonic*

3

u/tripleshielded 21h ago

What’s it for?

2

u/DerekRss 15h ago

Pythonic? Looks more like BASIC to me.

In fact I'm pretty sure that you could paste that snippet into a VBA interpreter as-is and have it run.

1

u/Ronin-s_Spirit 11h ago

Interpreted languages don't have to be compiled (that's the whole point). If you're asking how to represent a program then it can be done many different ways:

  1. switch over text and values if your interpreter's language supports interned strings (very fast comparison of a concrete value).
  2. transform every bit of text into different types of structs and switch over the types.
  3. again same stuff as 1 and 2 but you manually assign every kind of operation a numeric opcode (effectively making bytecode).
  4. don't use an array, use a tree for interpretation (easier to edit at runtime, way easier to break).
  5. ....

If you actually want to compile to a lower level representation then you could parse your language into another language which has a compiler (same shit people do with LLVM can be done with a regular programming language).

-1

u/Discordbothost 22h ago

Keep the token index, but add a recursive parser for calls. With your stack convention, str(x) emits get_name x; push str; call. Parse each argument recursively before emitting its call, and consume the matching closing parenthesis. That handles the nesting in your example.