This commit is contained in:
Vilyaem 2025-02-21 00:38:30 -05:00
commit edc2d4d19a
6 changed files with 80 additions and 0 deletions

9
README.md Normal file
View file

@ -0,0 +1,9 @@
# RPCALC
This program compiles reverse-polish-notation expressions to x86 16-bit
GNU-Assembly instructions. This is to be an educational supplement to teach
compiler theory.
## LICENSE
Public Domain.

4
c.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
rm rpcalc
cl
cc -O3 -std=c89 -Wall -Wextra -Wmain -Wpedantic -Werror main.c -o rpcalc && ./rpcalc

7
example.txt Normal file
View file

@ -0,0 +1,7 @@
100
100
+
200
200
+
*

5
example2.txt Normal file
View file

@ -0,0 +1,5 @@
1000
3
*
100
-

55
main.c Normal file
View file

@ -0,0 +1,55 @@
/*********************************************
* Description - RPCalc compiler
* Author - Vilyaem
* Date - Jun 05 2024
* *******************************************/
#include <stdio.h>
#include <string.h>
char* regnames[] = { "%ax","%bx","%cx","%dx" };
int main(void){
int stackptr = 0;
char strbuf[8];
char regnamebuf[16];
char regnamebuf2[16];
puts("/*Compiled with Vilyaem's simple rpcalc compiler*/");
while(fgets(strbuf,8,stdin) != NULL){
if(strbuf[0] == '\n'){
break;
}
strbuf[strlen(strbuf)-1] = '\0';
switch(strbuf[0]){
case '+':
strcpy(regnamebuf,regnames[stackptr-1]);
strcpy(regnamebuf2,regnames[stackptr-2]);
printf("add %%%s,%%%s\n",regnamebuf,regnamebuf2);
--stackptr;
break;
case '-':
strcpy(regnamebuf,regnames[stackptr-1]);
strcpy(regnamebuf2,regnames[stackptr-2]);
printf("sub %%%s,%%%s\n",regnamebuf,regnamebuf2);
--stackptr;
break;
case '*':
strcpy(regnamebuf,regnames[stackptr-1]);
strcpy(regnamebuf2,regnames[stackptr-2]);
printf("imul %%%s,%%%s\n",regnamebuf,regnamebuf2);
--stackptr;
break;
default: /*assume it is a numeric to be pushed*/
printf("mv $%s,%%%s\n",strbuf,regnames[stackptr]);
++stackptr;
break;
}
/*printf("# stackptr: %d", stackptr); for debugging*/
}
return 0;
}

BIN
rpcalc Executable file

Binary file not shown.