How 2 make a language?

tspier2 said:
Okay, I need to say something...You are all lazy bums, except for whomever asked the question.

Lazy bum...

moir???
incase you didn't read the whole thread I wrote a math processing language (as an example)...

and I wrote it in C...

Of course had I been a lazy bum I could have just downloaded someone elses language and added a few functions of my own...

Of course had I been that lazy it would have had to have been a language that was written in BASIC!


The thing about the questions like this is, those who ask them, don't know where to start and don't (usually) have a clue of the technologies involved to create a language... when they find out it'll be difficult they usually just give up and go away...
 
The Deitel & Deitel book about C Programming included an entire section on making your own complier. That's probably the place to start. But to make one's own programming language takes a while. It probably takes more planning than actual coding.
 
Here's a really basic compiler I made:
Code:
// Simpletron.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "Integer.h"

// 10:READ
// 11:WRITE
// 12:WRITE NEWLINE
// 20:LOAD
// 21:STORE
// 30:ADD
// 31:SUBTRACT
// 32:MULTUPLY
// 33:DIVIDE
// 34:MODULUS
// 40:BRANCH
// 41:BRANCHNEG
// 42:BRANCHZERO
// 43:HALT
// 44:DEBUG (00=no debug,01=all debug,02=register debug,03=memory debug)

using namespace std;

void Tokenize(const string &str, vector<string> &tokens, const string &delimiters);

int main(int argc, char* argv[]) {
	int accumulator = 0;
	int counter = 0;
	int choice = 0;
	int i = 0, j = 0;
	int temp = 0;
	float f_temp;
	int debug = 0;
	vector<string> toks;
	string code = "";
	int memory[1000];
	for (i=0;i<1000;i++) {
		memory[i] = 00000;
	}
	int operand, operationcode;
	ifstream infile;
	infile.open("smfile.txt");
	for (i=0;i<1000;i++) {
		if (!infile.eof()) {
			getline(infile,code);
			Tokenize(code,toks," ");
			//cout<< code << "\n";
			if (toks.size() == 2) {
				memory[counter] = atoi(toks[1].c_str());
			} else if (toks.size() == 3) {
				memory[counter] = atoi(toks[2].c_str());
			} else if (toks.size() == 1) {
				memory[counter] = atoi(toks[0].c_str());
			}
			//cout<< toks.size() << " " << memory[counter] << "\n";
			temp = toks.size();
			for (j=0;j<temp;j++) {
				toks.pop_back();
			}
			counter++;
		} else {
			break;
		}
	}
	/*cout<< counter << "\n";
	cout<<"MEMORY:\n";
	cout<<"      0    1    2    3    4    5    6    7    8    9\n";
	cout<<" 0";
	for (i=0;i<1000;i++) {
		if (j == 10) {
			cout<<"\n" << i+1;
			j = 0;
		}
		if (memory[i] < 10) {
			cout<<" 000" << memory[i];
		} else if (memory[i] < 100) {
			cout<<" 00" << memory[i];
		} else if (memory[i] < 1000) {
			cout<<" 0" << memory[i];
		} else {
			cout<<" " << memory[i];
		}
		j++;
	}
	for (i=0;i<1000;i++) {
		if (counter < 10) {
			cout<<"0" << counter << "? ";
			cin>> choice;
			//cout<<"\n";
			if (choice == 99999) {
				break;
			} else {
				memory[counter] = choice;
				counter++;
			}
		} else {
			cout<< counter << "? ";
			cin>> choice;
			//cout<<"\n";
			if (choice == 99999) {
				break;
			} else {
				memory[counter] = choice;
				counter++;
			}
		}
	}*/
	if (counter == 0) {
		cout<<"*** ERROR: PROGRAM COULD NOT BE ENTERED ***\n";
		goto exit;
	}
	cout<<"***   Program entering ends  ***\n";
	cout<<"*** Program execution begins ***\n";
	counter = -1;
	i = 0;
	while (i == 0) {
		counter++;
		//cout<<"\n" << counter << "\n\n";
		f_temp = memory[counter]/1000;
		operationcode = f_temp;
		operand = memory[counter];
		operand -= operationcode*1000;
		//cout<< operationcode << operand << "\n";
		if (operationcode == 10) {
			cout<<"? ";
			cin>> choice;
			memory[operand] = choice;
		} else if (operationcode == 11) {
			cout<< memory[operand] << "\n";
		} else if (operationcode == 12) {
			cout<<"\n";
		} else if (operationcode == 20) {
			accumulator	= memory[operand];
		} else if (operationcode == 21) {
			memory[operand] = accumulator;
		} else if (operationcode == 30) {
			accumulator += memory[operand];	
		} else if (operationcode == 31) {
			accumulator -= memory[operand];	
		} else if (operationcode == 32) {
			accumulator *= memory[operand];	
		} else if (operationcode == 33) {
			accumulator /= memory[operand];	
		} else if (operationcode == 34) {
			accumulator %= memory[operand];	
		} else if (operationcode == 40) {
			//cout<<"\n0\n\n";
			counter = operand-1;
		} else if (operationcode == 41) {
			//cout<<"\n1\n\n";
			if (accumulator < 0) {
				//cout<<"oper: " << operand << "\n";
				counter = operand-1;
			}
		} else if (operationcode == 42) {
			//cout<<"\n2\n\n";
			if (accumulator == 0) {
				counter = operand-1;
			}
		} else if (operationcode == 43) {
			break;
			cout<<"*** Execution terminated ***\n";
		} else if (operationcode == 44) {
			debug = operand;
		}
		if (debug == 1) {
			cout<<"REGISTERS:\n";
			cout<<"accumulator: " << accumulator << "\n";
			cout<<"counter:     " << counter << "\n\n";
			cout<<"MEMORY:\n";
			cout<<"      0    1    2    3    4    5    6    7    8    9\n";
			cout<<" 0";
			for (i=0;i<1000;i++) {
				if (j == 10) {
					cout<<"\n" << i;
					j = 0;
				}
				if (memory[i] < 10) {
					cout<<" 000" << memory[i];
				} else if (memory[i] < 100) {
					cout<<" 00" << memory[i];
				} else if (memory[i] < 1000) {
					cout<<" 0" << memory[i];
				} else {
					cout<<" " << memory[i];
				}
				j++;
			}
		} else if (debug == 1) {
			cout<<"REGISTERS:\n";
			cout<<"accumulator: " << accumulator << "\n";
			cout<<"counter:     " << counter << "\n\n";
		} else if (debug == 2) {
			cout<<"MEMORY:\n";
			cout<<"      0    1    2    3    4    5    6    7    8    9\n";
			cout<<" 0";
			for (i=0;i<1000;i++) {
				if (j == 10) {
					cout<<"\n" << i;
					j = 0;
				}
				if (memory[i] < 10) {
					cout<<" 000" << memory[i];
				} else if (memory[i] < 100) {
					cout<<" 00" << memory[i];
				} else if (memory[i] < 1000) {
					cout<<" 0" << memory[i];
				} else {
					cout<<" " << memory[i];
				}
				j++;
			}
		}
		if (counter >= 1000) {
			cout<<"*** ERROR: RAN OUT OF MEMORY. ***\n";
			break;
		}
	}
	system("pause");
	cout<<"REGISTERS:\n";
	cout<<"accumulator: " << accumulator << "\n";
	cout<<"counter:     " << counter << "\n\n";
	cout<<"MEMORY:\n";
	cout<<"       0     1     2     3     4     5     6     7     8     9\n";
	cout<<" 0";
	j = 0;
	for (i=0;i<1000;i++) {
		if (j == 10) {
			cout<<"\n" << i;
			j = 0;
		}
		if (memory[i] == 0) {
			cout<<" 00000";
		} else if (memory[i] < 10) {
			cout<<" 0000" << memory[i];
		} else if (memory[i] == 10) {
			cout<<" 000" << memory[i];
		} else if (memory[i] < 100) {
			cout<<" 000" << memory[i];
		} else if (memory[i] == 100) {
			cout<<" 00" << memory[i];
		} else if (memory[i] <= 1000) {
			cout<<" 0" << memory[i];
		} else {
			cout<<" " << memory[i];
		}
		j++;
	}
	cout<<"\n";
	system("pause");
exit:;
	return 0;
}

void Tokenize(const string& str, vector<string>& tokens, const string& delimiters) {
	string::size_type lastPos = str.find_first_not_of(delimiters,0);
	string::size_type pos     = str.find_first_of(delimiters,lastPos);
	while (string::npos != pos || string::npos != lastPos) {
		tokens.push_back(str.substr(lastPos,pos-lastPos));
		lastPos = str.find_first_not_of(delimiters,pos);
		pos = str.find_first_of(delimiters,lastPos);
	}
}

This was really easy to make. Feel free to hack into it, I don't really care.
This was the result of a question from C++ How to Program 3rd edition from Deitel and Deitel.
 
Back
Top Bottom