If you are going to create your own programming language, how will it look like?
I will create one based on Perl, it requires less keystrokes.
Hello World
Code:
print 'Hello, world!' # Prints "Hello, world!"
# or
$str = 'Hello, world'
print $str
# even variable functions
$print = "print"
$print{$str}
# $print $str will be treated as $print * $str
Quote styles (uses UNICODE)
Code:
print q?Hello, world!?
If/else
Code:
? $name == 'Naruto' print "Your name is Naruto, right?"
??$name == 'Sasuke' print "Hi, Sasuke"
?? { # sometimes you must use braces to tell the parser it is part of if/else
print "Who are you?" $name = <STDIN # no need to use > at last. Also, semicolon can be omitted if it does not confuse the parser
}
I/O Streams
Code:
open FILE, "file.txt"
?* <FILE {# $_ will be automatically assigned, ?* means "while"
>STDERR "Still more line: $line" # '>' operator writes to a writeable stream
$line++; # It doesn't need to be declared, like in Perl
}
close FILE
Variable-variables
Code:
# You can use expression after $ to get a variable-variable
$("var${counter}") = 100;
Associated references
Code:
# When you are assigning a value, you can use expressions and conditions to get lvalue
$var || $var2 = 100 #will set $var or $var2 to 100
1 + 1 = 1 # sets $_ to 1, the left hand side does not associate to any reference
$a + $b = 1 # sets $b to 1
print("Hello, world!") = 1 # print does not return reference, so this is like the previous one
Regexp
Code:
$_ = 'string';
?/string/ print "Matched."
Other ideas I've compiled to one text file:
Code:
Comments
# line comment
#> block comment <#
Operators
1 + 1 # 2
15 - 3 # 12
2 * 5 # 10
2 5 # NOT ALLOWED: two statements of: 2; 5;
2(5) # 10
10 / 5 # 2
-1.5 # -1.5
(1 + 5) * 2 # 12
$bool = 0; !$bool # true
$a || $b # or operator
$a && $b # and operator
$a | $b # bitwise or
$a & $b # bitwise and
$a ^ $b # NOT exponent, bitwise XOR
~$b # bitwise NOT
cos sin 10 # unary functions, same as: cos(sin(10))
$ "var" # identifiers are also strings, $ is an unary operator
$$var # variable-variables
$ "value-\{$var - 1}" # String variable-variable and interpolation
Functions
print "Hello, world!", " $name" # Top-level expressions can have multiple arguments
print push $array, 12 # print(push $array), 12 Doesn't work
print (push $array, 12) # parens embed Expressions
print cos rad $value # cos and rad are treated like unary operators
?& getmax $a $b {
?. # return
? $a > $b $a # embedded if/else
?? $b
}
# one-expression functions
?& getmax $a $b : ? $a > $b : $a ?? $b
?&getmax$a$b?:?$a>$b$a??$b # Compressed
Goto labels
label: # sets a label
?^label # goto a label
If/Else
? $a > 90 : print "You got A" # if (':' after condition may be optional)
?: $a > 80 : print "You got B" # else if
?? print "You got C" # else
? $answer == 2 print "You are right"
?? print "You are wrong"
# or ?? { print "You got C" }
? !$name {
print "Enter your name:\n"
$name = input()
?^ begin
}
While loops
$b = rand 100 # random numbers from 0 to 100
?* $a != $b {
print "Incorrect, try again."
$a = <STDIN;
}
For loops
?% $a($b) # foreach $a in $b
print $a # Remember braces can be omitted sometimes
?% $b print $_ # foreach $_ in $b
?% 1..10 print $_ # for $_ = 1 to 10
?% $j : 1..10..2 : print $j # for $j = 1 to 10 step 2
?% $i=0;$i<0;$i++ # Sometimes you can omit ; between statements
print $i
Keywords
?& function
? if
?: else if
?? else
?* while
?% for
?^ goto