r/ProgrammingLanguages • • Oct 30 '23

Am I doing this right?

I am making an interpreter for my custom programming language and I want to know if I am doing something wrong?

Link: https://docs.google.com/document/d/1ZSGVc1WnsMTxjiMUOrpd-v5EUI31tmX-bTCErYSTqR8/edit?usp=sharing

Edit: Here is the github repo https://github.com/doace4/My-Programming-Language

2 Upvotes

15 comments sorted by

19

u/bl4nkSl8 Oct 30 '23

Uh, can I recommend getting a GitHub account and using GitHub gists or GitHub repos.

Pretty much anything other than a screenshot is going to help us help you better :)

3

u/Doace2 Oct 30 '23

Oh sorry I am new at this.

3

u/bl4nkSl8 Oct 30 '23

All good! The tools I mentioned will make it easier for you and we'll be able to read the code and help you fix it :)

Looking forward to it (I couldn't read the code in your screenshot but would like to help)

Edit: Oh I see the new link. Awesome!

5

u/redchomper Sophie Language Oct 30 '23

You do you, but a common choice is to write down a few things about the PL before or alongside any code. For instance, what context does the PL fit into? What will it look like in writing? What special goals is it aimed at? Who is the (ideal) intended audience?

There are no wrong answers to these questions. But non-answers tend to result in aborted projects. And furthermore, that context would enable us to devise sensible feedback other than formulaic "keep practicing"-type advice.

5

u/Doace2 Oct 30 '23

I have no idea why I started to make this

2

u/[deleted] Nov 03 '23

Your programming language don't need to have a goal. If you just writing a hobby language then knowing how will your language looks like would help.

1

u/Doace2 Nov 03 '23

That makes sense

2

u/bl4nkSl8 Oct 30 '23

Okay, next step. Making that repo public will let us see the code at the link :)

2

u/Doace2 Oct 30 '23

Oh forgot that

2

u/Doace2 Oct 30 '23

Okay I think I did it

3

u/bl4nkSl8 Oct 30 '23

Great! It worked. Alright. Now I need to read the code and think for a bit

6

u/koflerdavid Oct 30 '23

I immediately noticed that you use two different ways to locate the keyword.

When you search for the keyword, the interpreter doesn't care where in the line it is located. But when it actually detects it, it immediately assumes that it is located before the first colon in the line.

IMHO, you should split the line and get the keyword first, as that is common to all branches there (although set is different...) This way, you also don't have to worry about what happens if the keyword is contained elsewhere in the line.

Another suggestion: use a separate dictionary instead of globals() to store variables. Else the script can mess with the interpreter's data structures in a way you might not have intended :-)

Keep going!

5

u/beephod_zabblebrox Oct 30 '23

there isn't any program.txt in the repo, btw (would be nice to have an example!)

not necessarily wrong, but you're using eval, which is kinda cheating.

also, what if i write a line with two spaces after "set"? maybe try to split a line on just whitespace (python has .split() with no arguments) and see what the first item is, instead of checking for a word.

try to see what happens when you have a line like this: condition: 2 in set()

oh and a python thing: you don't have to use contains, you can do "set" in code instead

3

u/KingJellyfishII Oct 30 '23

So you're definitely on the right track, if you want to see the theory behind how some other interpreters work I'd recommend looking into lexing, parsing, and abstract syntax trees (AST). That sounds scary but it's not too bad.

oh btw, I noticed you used a lot of a.__contains__(b), what about doing b in a instead? I believe they are equivalent in python and idk it might look cleaner or something EDIT: Nevermind I see you've already fixed it lol

2

u/Middle_Unlikely Oct 30 '23

Just keep going until you hit a wall :) then fix it and repeat

There are some obvious differences from the classic approach such as the lack of an explicit lexing stage,

But hey maybe the conventional approach isn't required for the way your language works.

One other thing I'd say is for performance reasons you might be better of iterating by character rather than doing things like split() - which heap allocate memory as far as I know, but you'd need to test this.