patternpythonModerate
Making my own programming language
Viewed 0 times
makinglanguageownprogramming
Problem
This question is really to help me decide on something. I have started development of my own programming language that I am calling DeliciousWaffle (or maybe samscript).
So far it looks pretty cool. I built a compiler in Python, and associated
Here are a few examples of what it looks like:
The fully functional IDE:
A calculator program that I wrote in DeliciousWaffle:
And here is the code for the compiler. You can download this if you'd like and try out the syntax.
```
#deliciouswaffle
import sys
import linecache
varss={}
def Say(vartype,text):
if vartype=='normal':
print text
if vartype=='var':
print varss.get(str(text))
def Set(var,val):
global varss
varss.update({str(var):val})
def Get(var,prmpt):
val=raw_input(prmpt)
try:
val=int(val)
x=val+1
except:
val=str(val)
Set(var,val)
def Loop():
global line
line=0
def IfStr(var1,functlines):
global line
var=varss.get(var1)
try:
x=var+1
line+=functlines
except TypeError:
pass
def IfInt(var1,functlines):
global line
var=varss.get(var1)
try:
x=var+1
pass
except TypeError:
line+=functlines
def Convert(var):
val=varss.get(var)
if type(val) == type('str'):
val=int(val)
if type(val) == type(1):
val=str(val)
varss.update({var:val})
def If(var1,compare,var2,functlines):
global varss
global line
var1=varss.get(str(var1))
var2=varss.get(str(var2))
if compare=='equ':
if var1==var2:
pass
else:
line+=functlines
if compare=='lss':
if var1var2:
pass
else:
line+=functlines
def ChangeVar(var,operation,do):
global varss
val=varss.get(var)
if operation=='+':
So far it looks pretty cool. I built a compiler in Python, and associated
.dw files with it. These files are like .py or .vbs or .java. They provide the code for the compiler to interpret. I also programmed an IDE for it using wxPython.Here are a few examples of what it looks like:
The fully functional IDE:
A calculator program that I wrote in DeliciousWaffle:
And here is the code for the compiler. You can download this if you'd like and try out the syntax.
```
#deliciouswaffle
import sys
import linecache
varss={}
def Say(vartype,text):
if vartype=='normal':
print text
if vartype=='var':
print varss.get(str(text))
def Set(var,val):
global varss
varss.update({str(var):val})
def Get(var,prmpt):
val=raw_input(prmpt)
try:
val=int(val)
x=val+1
except:
val=str(val)
Set(var,val)
def Loop():
global line
line=0
def IfStr(var1,functlines):
global line
var=varss.get(var1)
try:
x=var+1
line+=functlines
except TypeError:
pass
def IfInt(var1,functlines):
global line
var=varss.get(var1)
try:
x=var+1
pass
except TypeError:
line+=functlines
def Convert(var):
val=varss.get(var)
if type(val) == type('str'):
val=int(val)
if type(val) == type(1):
val=str(val)
varss.update({var:val})
def If(var1,compare,var2,functlines):
global varss
global line
var1=varss.get(str(var1))
var2=varss.get(str(var2))
if compare=='equ':
if var1==var2:
pass
else:
line+=functlines
if compare=='lss':
if var1var2:
pass
else:
line+=functlines
def ChangeVar(var,operation,do):
global varss
val=varss.get(var)
if operation=='+':
Solution
I second the recommendation to invent a grammar and use a real parser. A recursive descent parser is very easy to write, and a great place to start. You might also have a look at a PEG (Parsing Expression Grammar). It's almost as easy to use a PEG as it is to write a recursive descent compiler, and a PEG can parse more grammars than can a recursive descent compiler.
Here are two PEGs for Python (I haven't used either of them, so can't vouch for them):
I don't see a good reason to get the Dragon book yet. Much of what it teaches is how to write the LEXX/YACC type tools, along with language theory. I think it's too deep and too low level for someone who just wants to try creating a language in order to see how it's done. If, however, you do want to dive that deep, it is a very good book.
I would, for now, steer clear of a traditional lex/yacc setup. That's a harder road that can wait until you've had some experience with simpler schemes. However, if you do want to go there, PLY is a lex/yacc type parser for Python.
Here are two PEGs for Python (I haven't used either of them, so can't vouch for them):
- pyPeg
- parsimonious
I don't see a good reason to get the Dragon book yet. Much of what it teaches is how to write the LEXX/YACC type tools, along with language theory. I think it's too deep and too low level for someone who just wants to try creating a language in order to see how it's done. If, however, you do want to dive that deep, it is a very good book.
I would, for now, steer clear of a traditional lex/yacc setup. That's a harder road that can wait until you've had some experience with simpler schemes. However, if you do want to go there, PLY is a lex/yacc type parser for Python.
Context
StackExchange Code Review Q#39565, answer score: 17
Revisions (0)
No revisions yet.