How do these specs look?
Zavexeon (237)

Hiya. So, I'm creating my own programming language, Zwei, and I've written a sample spec file for how it's going to look. Before I start making the compiler, I want to make sure that the language is appealing to others. I have a few examples of what the syntax will look like below. I appreciate feedback.

Zwei Specs

#---------------------------------------------------------------------------
#variable declaration; 

my_int = 1232;
my_bool = true; #can be false or null, as well
my_str = 'sample_string'; #double quotes work as well
my_ary = [item1, item2, item3];
my_hash = [item1 => val1, item1 => val2]

#---------------------------------------------------------------------------
#declaring and calling a function

func func_name {
    #function without parameters
}

func func_name: var1, var2 {
    #function with parameters 
}

func_name; #calling function without params 
func_name: var1, var2; #calling function with params
func_name(var1, var2) #alternate method of calling func with params

#example

func greet: name {
    io.out: "Hello, #{name}!" #or io.out: "Hello, " + name + "!";
}

greet: "Bly"; #or greet("Bly");

#---------------------------------------------------------------------------
#control flow 

#if, else, elsif statement 
if: true_statement {                   #if(true_statement) works

} elsif: true_statement {              #elsif(true_statement) works

} else {

}

#example
weather = 'snowy';

if: weather == 'sunny' {
    io.out('It's sunny outside!'); #remember, this can also be written io.out: 'sample text';
} elsif: weather == 'rainy' {
    io.out('It's rainy.');
} elsif: weather == 'snowy' {
    io.out('It's snowy.');
} else {
    io.out('I'm not sure about the weather.');
}


#case statement 
case: argument { #case(argument) works
    when: statement { #when(statement) works

    }
    else {

    }
}

#example 
age = 16; 

case: age {
    when: _ < 13 {
        io.out("I'm still a little kid!");
    }
    when: _ >= 13 {
        io.out("I'm a teenager!");
    }
    when: _ > 19 {
        io.out("I'm older than a teenager!");
    }
    else {
        io.out("I'm not sure of the age.");
    }
}

#-----------------------------------------------------------------------
#looping

#while loop
while: true_statement { #while(true_statement) works
    #repeated action
} 

#while loop with break


while: true_statement {
    #repeated action
    
    #break should be the last line
    breakif: true_statement; #breaks the loop it is held within, breakif(true_statement) works 
}

#for loops 
for: my_array |obj| { #for(array) |obj| works
    #manipulate obj here 
}

for: my_hash |key, val| { #for(hash) |key, val|
    #manipulate key and val
}
#the io object you keep seeing is used to manipulate the console 
io.out(); #outputs an object such as a string, integer, etc
io.in(); #takes string input (takes one optional argument to try to automatically convert the string input into the object specified, such as an integer, boolean, etc).

I did notice there are some inconsistencies with the io.out function in my specs Functions can be written like this io.out(args) or like this io.out: args.

You are viewing a single comment. View All
Answered by vedprad1 (691) [earned 5 cycles]
View Answer
theangryepicbanana (688)

I do like the idea, but you should probably fix

case: age {
} when: age < 13 {
    io.out("I'm still a little kid!");
} when: age >= 13 {
    io.out("I'm a teenager!");
} when: age > 19 {
    io.out("I'm older than a teenager!");
} else {
    io.out("I'm not sure of the age.");
}

because having an empty block is kinda odd (and ugly) imo, and the age at the start is redundant if you have to use it in the when statements. maybe you could instead have something like

case: age {
    when: _ < 13 {
        io.out("I'm still a little kid!");
    }
    when: _ >= 13 {
        io.out("I'm a teenager!");
    }
    when: _ > 19 {
        io.out("I'm older than a teenager!");
    }
    else {
        io.out("I'm not sure of the age.");
    }
}
theangryepicbanana (688)

Also, you should figure out how all of this is going to work internally (unless the language isn't self-hosted)

Zavexeon (237)

@theangryepicbanana That's actually really good feedback. I'll adjust that.

vedprad1 (691)

@Zavexeon :
Actually, I just realized that there is a problem in your case statements (not the language, just the code). The case will always be in one of the first two, because even if your past a teeneager, your age is still greater than 13.

Zavexeon (237)

@vedprad1 Oh crap. Yeah I should reverse the order of the case statements. I've done that before in other languages but fixed it when I wasn't getting the desired result.

But I'm lazy and I don't feel like reversing it here.