Hey, guys!
So, I was wondering, has anybody else here made their own programming language?
Thus far, I've made one programming language. It's a simplified low-level programming language, I call it AEC (Arithmetic Expression Compiler). Right now, it's not a very useful language, many things need to be done in inline Assembly even for simplest programs. Here is an example program in it, one that prints the Pascal's Triangle:
;Pascal's triangle
AsmStart
macro pushIntegerToTheSystemStack decimalNumber
{
sub esp,4
fld dword [decimalNumber]
fistp dword [esp] ;"fistp" is the x86 assembly language directive for converting decimal numbers to integers.
}
macro pushPointerToTheSystemStack pointer
{
sub esp,4
lea ebx,[pointer]
mov [esp],ebx
}
macro pushStringToTheSystemStack string
{
sub esp,4
mov dword [esp],string
}
format PE console ;"PE" means 32-bit Windows executable.
entry start
include 'win32a.inc' ;FlatAssembler macros for importing functions from DLLs.
section '.text' code executable
start:
jmp howManyRowsString$
howManyRowsString:
db "How many rows of Pascal's triangle do you want to be printed?",10,0 ;10 is '\n', and 0 is '\0'.
howManyRowsString$:
pushStringToTheSystemStack howManyRowsString
call [printf] ;printf(howManyRowsString)
jmp theFloatSymbol$
theFloatSymbol:
db "%f",0
theFloatSymbol$:
pushPointerToTheSystemStack numberOfRows
pushStringToTheSystemStack theFloatSymbol
call [scanf] ;scanf(theFloatSymbol,&numberOfRows)
AsmEnd
currentRow := 0
While currentRow < numberOfRows | currentRow = numberOfRows
AsmStart
jmp currentRowString$
currentRowString:
db "Row #%d:",9,0 ;9 is '\t' (the tabulator).
currentRowString$:
pushIntegerToTheSystemStack currentRow
pushStringToTheSystemStack currentRowString
call [printf] ;printf(currentRowString,currentRow)
AsmEnd
currentColumn:=0
While currentColumn < currentRow | currentColumn = currentRow
If currentColumn = 0
array(currentRow * 2 * numberOfRows + currentColumn) := 1 ;When I haven't programmed the compiler to deal with 2-dimensional arrays...
ElseIf currentColumn = currentRow
array(currentRow * 2 * numberOfRows + currentColumn) := 1
Else
numberImmediatelyAbove := array((currentRow - 1) * 2 * numberOfRows + currentColumn)
numberBeforeTheImmediatelyAboveOne := array((currentRow - 1) * 2 * numberOfRows + currentColumn - 1)
array(currentRow * numberOfRows * 2 + currentColumn) := numberBeforeTheImmediatelyAboveOne + numberImmediatelyAbove
EndIf
numberToBePrinted := array(currentRow * numberOfRows * 2 + currentColumn)
AsmStart
jmp integerSignWithTabulator$
integerSignWithTabulator:
db "%d",9,0 ;"%d\t"
integerSignWithTabulator$:
pushIntegerToTheSystemStack numberToBePrinted
pushStringToTheSystemStack integerSignWithTabulator
call [printf] ;printf(integerSignWithTabulator,numberToBePrinted)
AsmEnd
currentColumn := currentColumn + 1
EndWhile
AsmStart
jmp newLineString$
newLineString:
db 10,0 ;"\n"
newLineString$:
pushStringToTheSystemStack newLineString
call [printf] ;printf(newLineString)
AsmEnd
currentRow := currentRow + 1
EndWhile
AsmStart
pushStringToTheSystemStack pauseString
call [system] ;system(pauseString)
invoke exit,0 ;exit(0)
pauseString db "PAUSE",0
section '.rdata' readable writable
result dd ? ;A variable used internally by the AEC compiler.
numberOfRows dd ?
currentRow dd ?
currentColumn dd ?
numberBeforeTheImmediatelyAboveOne dd ?
numberImmediatelyAbove dd ?
numberToBePrinted dd ?
array dd 30000 DUP(?)
section '.idata' data readable import
library msvcrt,'msvcrt.dll' ;"Microsoft Visual C Runtime Library", available as "C:\Windows\System32\msvcrt.dll" on Windows 98 and newer.
import msvcrt,printf,'printf',system,'system',exit,'exit',scanf,'scanf',clock,'clock'
AsmEnd
The executable file is available in the ZIP-archive
here.
The most complicated thing I've done in AEC is implementing a sorting algorithm I came up with, it combines QuickSort and MergeSort depending on what seems optimal. You can see the source code
here (the comments and variable names are in Croatian), the Assembly code (compatible with FlatAssembler) my compiler produces for it is available
here, and the executable is available
here. I've written a
paper about that and I hope it will get published in Osječki Matematički List.
So, does anybody else here have some similar experience?