19
\$\begingroup\$

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 .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:

DWIDE

A calculator program that I wrote in DeliciousWaffle:

Calculator

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 var1<var2:
 pass
 else:
 line+=functlines
 if compare=='gtr':
 if var1>var2:
 pass
 else:
 line+=functlines
def ChangeVar(var,operation,do):
 global varss
 val=varss.get(var)
 if operation=='+':
 val+=do
 if operation=='-':
 val-=do
 if operation=='/':
 val/=do
 if operation=='*':
 val*=do
 varss.update({var:val})
def CombineVars(newvar,var1,var2,operation):
 global varss
 var1=int(varss.get(var1))
 var2=int(varss.get(var2))
 if operation=='+':
 val=var1+var2
 if operation=='-':
 val=var1-var2
 if operation=='/':
 val=var1/var2
 if operation=='*':
 val=var1*var2
 varss.update({newvar:val})
def End():
 global eof
 eof=1
line=0
eof=0
try:
 program = (sys.argv[1])
except:
 program = 'main.dw'
if program == 'main.dw':
 try:
 f=open('main.dw','r')
 f.close()
 except:
 print 'No Delicious Waffle script loaded.'
 print 'Create a script called main.dw, or load one into Delicious Waffle.'
 raw_input()
 sys.exit()
while eof==0:
 try:
 line+=1
 try:
 eval(linecache.getline(program, line))
 except:
 pass
 except:
 eof=1
 f=open('tempvars.file','w')
 f.close()
 sys.exit()

What do you think? Is this worth completing? Or should I leave it be? This question is probably inappropriate for code review, sorry.

Phrancis
20.5k6 gold badges69 silver badges155 bronze badges
asked Jan 18, 2014 at 21:43
\$\endgroup\$
1
  • \$\begingroup\$ compiler to interpret - Umm... compilers don't interpret stuff. Please correct such core terminology issues. This should help clear it up. \$\endgroup\$ Commented Apr 5, 2016 at 5:16

2 Answers 2

17
\$\begingroup\$

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.

answered Jan 19, 2014 at 1:35
\$\endgroup\$
4
  • \$\begingroup\$ I don't mean to be a nuisance, but I am not understanding how to use the pasrsers.. would you mind explaining how I transcode that to interpret my own syntax? I read up on all the pages you've suggested.. and it isn't making sense. \$\endgroup\$ Commented Jan 19, 2014 at 1:58
  • 1
    \$\begingroup\$ @Sam, I started to, but it's too big a subject. I think I would recommend starting with a recursive descent compiler, though. That's the simplest thing. \$\endgroup\$ Commented Jan 19, 2014 at 2:43
  • 2
    \$\begingroup\$ @SamTubb How to use parsers? "The GOLD Parsing System" is a parser which doesn't have a python engine but which IMO has good documentation: the overview is that you a) define a "grammar" for your language; b) feed the grammar, plus some text (e.g. "source code" or "script") written into the language, into the parser c) the parser outputs an "abstract syntax tree" version, which is (theoretically) easier for you to process than the original script text. The grammar syntax is parser-specific. \$\endgroup\$ Commented Jan 19, 2014 at 15:52
  • \$\begingroup\$ I understand now! But I think I'm just going to stick to my method for now. Maybe if this ever becomes serious I'll use pyPeg, I like that one. \$\endgroup\$ Commented Jan 19, 2014 at 19:06
10
\$\begingroup\$

It is a beginning. But currently your code is just a thin wrapper around Python function calls. And of course, there is a security problem with "eval", because someone could format your harddisk with the right line, if you execute scripts from untrusted sources.

Maybe you should invent a nice syntax for your language. An easy method to write a parser for it, would be a top-down parser (or recursive descent parser), for simple expressions as demonstrated here.

Later, you could even build a parser tree and convert it to assembly. The so-called "dragon book" is the best reference, if you want to know more about building parsers and compilers.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Jan 18, 2014 at 22:10
\$\endgroup\$
3
  • \$\begingroup\$ I've heard people recommend "the dragon book" but didn't find it practical. Perhaps more practical if you want to write a parser is to learn to use a well-known tool like "YACC"; or I chose a less-well-known but perhaps-easier tool, the "GOLD Parsing System". They're for parsing relatively complicated programming languages, though. \$\endgroup\$ Commented Jan 18, 2014 at 22:16
  • \$\begingroup\$ I might suggest one of the Modern Compiler Design in [ML|C|Java] by A. Appel slightly more direct books and just as rewarding for most \$\endgroup\$ Commented Jan 19, 2014 at 5:12
  • 1
    \$\begingroup\$ I think I'm just going to stick to the thin wrapper for now, thank you for your help though. At least I learned something from this. \$\endgroup\$ Commented Jan 19, 2014 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.