Custom proggraming language

Pages: 12
closed account (NUj6URfi)
Anyone want to build one in c++ with me? I want to build an easy one with the print statement being print and I need help with variable declaration in the new language. My idea is that you load a text file into a terminal and it gets compiled and run in that terminal. Kind of like xcode which does the c++ code in a terminal.
Last edited on
So... Your language needs variables and a 'print' statement?
closed account (NUj6URfi)
No. I need help with the variables but the print statement I know how to do. I am curious if anyone wants to help make this language.
My point was, you told us nothing about the language. What makes it unique? What are it's defining features? What TYPE of language is it? You've said absolutely nothing about it.
closed account (NUj6URfi)
I want to make a language that would be completely easy for me to master because I wrote it and would be able to use any bit of c++ by adding a few lines of code that says for example:

1
2
3
if (sdl(square, 100, 100) == typed) {
//sdl stuff
}


I was curious if anyone wanted in because I don't understand how to do the trickier bits and my code already has errors. I have a file call Instructions.txt in the same folder as my cpp file and I type Instructions.txt into the program and nothing happens. Instructions.txt has this written:

 
print "It works :)!"


My code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

char filename[MAX_PATH + 1];
char input_line[MAX_PATH + 1];
int argvcount;
string randomaccess;
int i;

int main(int argc, char *argv[]) {
    cout << "What file do you want to compile?";
    cin.getline(filename, 100);
    ifstream file_in(filename);
    argvcount = 0; 
    i = 0;
    while( i <= argc) {
            i++;
            argvcount++;
            file_in.getline(input_line, MAX_PATH);
            if (argv[argvcount] == "print") {
               argvcount++;
               randomaccess = argv[argvcount];
               cout << randomaccess;
               }
               }
               cin.get();
               return 0;
               }
Last edited on
closed account (N36fSL3A)
You want to write a language without classes? lol.
You want to write a language without classes? lol.


Dumbest snide remark I've seen a while.

1. There are plenty of good non-OOP languages
2. Assuming he's making this for learning purposes, he can get plenty out of this not including OOP features.
closed account (N36fSL3A)
There are plenty of good non-OOP languages
Like?

C doesn't count. That's different.

That's really not very good practice, you wont get much from this once this starts looking like spaghetti code.
closed account (3qX21hU5)
Lumpin wrote:
Like?


Functional programming languages, scripting languages, markup languages, ect. ect. I could go into a list of all the languages but it would be a waste.

I agree with Austin it is one of the dumbest snide remarks I have seen in awhile also.

<Snide remark>Though it can be funny when beginners believe they know it all ;p </Snide remark>
Last edited on
@toad1359

Just some points about your code:

First is the use of global variables - don't do it. Put them in main(). It is also good practice not to have line 4.

Next is the use of the program arguments. I would have made the file name a program argument, rather than ask for it interactively. Or ask for it, only if it wasn't a program argument.

When using program arguments, always do error checking on them - the right number of them or anything else that has potential to cause problems.

Line 18 goes past the end of the argv array.

The next thing is you should be parsing the file - breaking it into tokens and going from there. But your line 22 has "print" as a program argument.

Hopefully this is useful for you - cheers :+)
Zereo: best laugh I've had all day
closed account (NUj6URfi)
I know about error checking. I want to create a full programming language and this is just the example I created in 2 mins.
closed account (NUj6URfi)
By example I mean that that is how the compiler would work. I can't do this without someone else. Anyone want in?
closed account (NUj6URfi)
So?
closed account (o1vk4iN6)
There are different ways to parse text but I know someone made a really simple one a while ago if you want to see how it works:

http://www.cplusplus.com/forum/lounge/11845/#msg56331
closed account (NUj6URfi)
I have heard remarks and questions yet does anyone want to help? xerzi and TheIdeasMan are the only ones who have actually even tried.

1. There are plenty of good non-OOP languages


+1, and I only add that having no classes *does not imply* being non-OOP. See JavaScript. No classes, but OOP.

Also, designing a programming language is in the same league as designing an operating system or a database engine. It *is* hard. Sure, you can design a new toy language as an excercise in CS / programming, but don't expect miracles.
Last edited on
You should take the first steps yourself, based on TheIdeasMan's suggestions. But I wouldn't class helios' LALR parser as really simple in a general sense (it's not beginners' code!), though it is pretty much as basic as you can get and still be an LALR parser (all it does is basic arithmetic.)

If you restrict your parser to deal with one simple statement per line then you can make the parsing far simpler (just a sequence of if statements) so you can get something up and running quickly. Then you can replace the parser with a proper one later on.

And if you make your language an interpreted scripting language to start with, rather than a compiled one, you can make things even easier. And get immediate feedback about what's going on, too.

Out of interest, what do you know about parsing so far? If you're new to it, you might want to check out this site:

Introduction to Parsing
http://www.mollypages.org/page/grammar/index.mp

Andy

PS I think you might need to brush up your C++ a bit before continuing...

Where hello.txt is just

print "Hello, world!"


the output of the adjusted code below is

What file do you want to compile?hello.txt
"Hello, world!"
<return> to exit


Adjusted code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

// MAX_PATH is a MSVC specific define
#ifndef MAX_PATH
#define MAX_PATH  260
#endif

char filename[MAX_PATH + 1];
char input_line[MAX_PATH + 1];
int argvcount;
string randomaccess;
int i;

int main(int argc, char *argv[]) {
    cout << "What file do you want to compile?";
    cin.getline(filename, 100);
    ifstream file_in(filename);
    argvcount = 0; 
    i = 0;
    // DOES NOT COMPUTE!
    //while( i <= argc) {
    while(!file_in.eof()) { // NOT THE BEST WAY... (do some readin up!)
        i++;
        argvcount++;
        file_in.getline(input_line, MAX_PATH);
        // 1. not the way to compare a C null-terminated string
        // 2. why are you comparing it with the command line arg?
        //    rather than the line you're just read?
        //if (argv[argvcount] == "print") {
        // use strncmp to check first 6 chars of input line (not a robust approach!)
        // http://www.cplusplus.com/reference/cstring/strncmp/
        if (strncmp(input_line, "print ", 6) == 0) {
            argvcount++;
            // why are you using the command line arg here?
            //randomaccess = argv[argvcount];
            // JUST FOR ILLUSTRATION (need to do proper string parsing)
            randomaccess = input_line + 6; // length of print plus one for space
            cout << randomaccess << "\n"; // added newline
        }
    }
    cout << "<return> to exit\n"; // let user know what to do
    cin.get();
    return 0;
}




Last edited on
closed account (NUj6URfi)
Thanks for the code. Now I am really moving along. I am adding newline statements.
closed account (NUj6URfi)
Though would strlen be the easiest way to determine the size of the text?
Pages: 12