So, you want to build a compiler huh? Well, You downloaded the right manual. This document will show you how to START a compiler, not build a full one. Please also note that this is NOT for beginners. Chapter 1 Planning Okay, now we need to plan what's going to be what. What the language is called, what language does it output, comments, that kind of stuff. Let's start by defining the language: Language name: Let's call it STAR, okay Output: What is it going to output? Hmmm... how about Qbasic code. Comments: We'll use the % sign for comments Statements: We'll borrow a couple statements from Qbasic INPUT for input PRINT for printing stuff on the screen PRINTVAR for printing variables on the screen VAR to declare variables And > to end the program Okay now that it's planned out, let's begin. Chapter 2 Beginning THE FOLLOWING IS QBASIC CODE: '---------------------------------------------- compname$ = "STAR to Qbasic compiler" cls print compname$ print "1.Begin programming" print "2.Quit" input choice if choice = 1 goto new endprog: end new: cls 'Stop cutting '--------------------------------------------- Q: What was that?!! A: That was the beginning of our compiler. It starts out with a basic menu The endprog label is there for future end statements Now we can continue with the editor THE FOLLOWING IS QBASIC CODE '-------------------------------------------------- cls input "File name? ", filname$ color 10,1 cls txt: input "",txt$ if txt$ = "" goto txt if txt$ = "%" goto comment if txt$ = "print" goto prnt if txt$ = "input" goto input if txt$ = "var" goto var if txt$ = "printvar" goto prntvar if txt$ = ">" goto ending 'Stop cutting '--------------------------------------------------- Okay, we now have our commands laid out. Now, we will have the compiler turn them into Qbasic code THE FOLLOWING IS QBASIC CODE '--------------------------------------------------- print "Syntax error" print "" play "d" goto txt comment: input txt$ close open filname$ for append as #1 print #1, "'";txt$ close goto txt prnt: input txt$ close open filname$ for append as #1 print #1, "PRINT ";chr$(34);txt$;chr$(34) close goto txt input: input txt$ close open filname$ for append as #1 print #1, "Input ";txt$ close goto txt 'Stop cutting '-------------------------------------------------- Now we have the basic PRINT, COMMENT and INPUT functions. Note: I'm not sure if chr$(34) = "" so if I'm wrong I'M SORRY! Note that the compiler saves to the program after every line. Now, we finish our compiler THE FOLLOWING IS QBASIC CODE '----------------------------------------------------------- var: input txt$ close open filname$ for append as #1 print #1, txt$ close goto txt prntvar: input txt$ close open filname$ for append as #1 print #1, "PRINT ";txt$ close goto txt ending: close open filname$ for append as #1 print #1, "'>" goto endprog 'Stop cutting '--------------------------------------------------------- Well, there ya have it. Test this compiler with this program % The classic (and cheesy) Hello world program Print Hello world! > Name the file HELLO.BAS because it's gonna be compiled by Qbasic eventually. This is about as simple as it gets. The CRAPPIEST and EASIEST compiler to ever be built. But it works (or at least it should.). There is a downloadable example of this. It's called the Saturn 1 to MOONROCK compiler version xxx, where xxx is the version. Download it from: www.qb4all.com in the Compilers section. Moonrock comes with it. Version 0.02 SUCKS but version 0.05 is a little better. Well, that just about ends it. Bye Bye!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Made on AUGUST 3rd 2004 by BRENDEN "b-man" HOMAN