Reputation: 24535
I read these pages on making a new language using tools available in Racket with great interest. It creates new language that can take numbers and operators in reverse polish notation and find the answer.
However, same task can be achieved by simple programming also. Following is code in Python but same can be in any general purpose language:
# Items for calculation:
slist=["4", "8", "+", "5", "*", "10", "/", "4", "-", "6", "5", "/", "*"]
# Main code:
mainstack = []
for item in slist:
print("mainstack: ", mainstack)
print("item: ", item)
if item in ['+','-','*','/']:
n2 = mainstack.pop()
n1 = mainstack.pop()
if item == '+': mainstack.append(n1 + n2)
elif item == '-': mainstack.append(n1 - n2)
elif item == '*': mainstack.append(n1 * n2)
elif item == '/': mainstack.append(n1 / n2)
else:
try:
mainstack.append(float(item))
except:
print("Invalid entry: ", item)
quit()
print("Final answer: ", mainstack)
Output:
mainstack: []
item: 4
mainstack: [4.0]
item: 8
mainstack: [4.0, 8.0]
item: +
mainstack: [12.0]
item: 5
mainstack: [12.0, 5.0]
item: *
mainstack: [60.0]
item: 10
mainstack: [60.0, 10.0]
item: /
mainstack: [6.0]
item: 4
mainstack: [6.0, 4.0]
item: -
mainstack: [2.0]
item: 6
mainstack: [2.0, 6.0]
item: 5
mainstack: [2.0, 6.0, 5.0]
item: /
mainstack: [2.0, 1.2]
item: *
Final answer: [2.4]
What are particular roles of special functions in Racket to make new languages? Why not simply use usual programming to convert specified domain-specific input into required outputs?
Upvotes: 1
Views: 68
Reputation: 48745
There is a difference. While you have written an interpreter for a language in Python. Making a parser for a surface language and translating it into a s-expressions that includes the base library of Racket basically means the resulting code is running native. You can compile the "stack code" and get a binary.
In Python you could write a calculator compiler that compiles the code to python and that allows dynamic compiling so that python creates the bytecode that runs the code in native, but it has no features to allow you to write in other languages in python.
Racket isn't one language but rather a lisp like Scheme derived dialect with systems to write a new language that in reality becomes a module under Racket. You can then write something in one language and perhaps import it as a library in another. It has the feel of Parrot where the idea was that Python and Perl would have the same runtime and that they could share libraries.
As an example you can load Algol60 in Racket, the most successful programming language designed by committee and the predecessor of most of todays programming languages. Python and C are both Algol dialects!
I bet you could write a Python parser and a python base library such that you can feed it the other libraries make in python and get a full python run under Racket. With that you could then just import other racket code as if it was written in Python and use it directly or you could import python libraries and use it in any of the other languages supported by Racket.
The only thing that would make this even sweeter would be if they could make Racket core under the nanopass compiler of Chez Scheme. Then you'll be able to compile whatever Racket supports to just about anything.
Upvotes: 2