GM Off Topic
September 08, 2010, 11:22:38 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Chat Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Concept for my own programming language.  (Read 700 times)
Somelauw
Newbie
*

Karma: 7
Offline Offline

Posts: 166


View Profile
« on: October 18, 2009, 06:28:40 pm »

I have thought about new original ideas for syntax in a programming language.
Here are some examples showing a couple of them.

The language has functions which look a bit like programmable syntax.
All variables are written between {} when on the left side and often between [] when on the right side (but not always).
I have also invented some kind of array, but I'm not really happy with it, so it's likely to change.
The difference between reals, strings and variables is a little vague probably, but that was done on purpose.

Please tell me what you think of the syntax so far.

Code:
Hello world
===========
print {Hello World!}

Fabonacci
===========
{a} = {0}
{b} = {1}

while {1}:
     print {b}
     {b} = % + [a]
     end


Faculty
===========
{a} = {1}
{b} = {1}
while {1}:
     print{a}
     {b} = % + {1}
     {a} = % * [b]
     end

The holy answer
============
{The answer to life, universe and everything} = {42}
print {5} * [The answer to life, universe and everything]

//This next line should be read as "_42 = (5+1) * (9-2);"
{42} = {5}+{1} * {9}-{2}                                 
print [42]{ = 42}

Fill an array with the complement of the index to 10
============
{x} iters Range{{0}{10}{1}}:
     {array}[x] = {10} - [x]
     end

Define pow as a function to calculate powers for ints
============
def {x} pow {y}:
     {ans} = 1
     {i} iters Range{{0}, }[y]{, {1}}
          {ans} = %* {x}
          end
     end

//Now we calculate 5 ^ 4
print {5}pow{4}

Programming the prefix/postfix ++ operator.
============
def {var}++{var2}:
     [var] =% + 1
     if  {var} and not{var2}
          return [var] - {1}
     else if  not{var} and {var}
          return [var]
          end
     end

[a] = 1
{b} = {a}++
// a = 2
// b = 1

{b} = ++{a}
// a = 2
// b = 2
Logged

Bami
Jr. Member
**

Karma: 68
Offline Offline

Posts: 924


imageshack is ded?


View Profile WWW
« Reply #1 on: October 18, 2009, 07:18:02 pm »

So you've created a weak-typed language with random syntax?

What does the % imply? Is it a modulo operator?
Code:
Fabonacci
===========
{a} = {0}
{b} = {1}

while {1}:
     print {b}
     {b} = % + [a]
     end
What does this do?
a remains zero, so it just prints 1,1,1,1,1,1,1,1

Why does a while loop accept a number? Why are there curly brackets around function parameters?
I really don't know what you're doing here, it's just a big bag of WTF to me.
Logged
Somelauw
Newbie
*

Karma: 7
Offline Offline

Posts: 166


View Profile
« Reply #2 on: October 18, 2009, 07:41:42 pm »

So you've created a weak-typed language with random syntax?
Yes. It's very weaktyped.
I didn't explain everything precise, because I was wondering how intuitive my language would be.
But it is not really random or ambiguous.

Quote
What does the % imply? Is it a modulo operator?
% refers to the value of the variable on the left side of the =.
So
Code:
{b} = % + a
would be similair to:
Code:
b += a;
in gml, java, etc.
I forgot this was in conflict with the modulo operator, so I may change it or use something else as a modulo operator.

Quote
Code:
Fabonacci
What does this do?
a remains zero, so it just prints 1,1,1,1,1,1,1,1
It's supposed to print: 1, 1, 2, 3, 5, 8...

Quote
Why does a while loop accept a number? Why are there curly brackets around function parameters?
I really don't know what you're doing here, it's just a big bag of WTF to me.
If 1 is passed to a while loop, it's an infinite loop.

For curly brackets there is a rule like follows in my language:
On the left side of the =, it's a variable
On the right side, everything between is a literal which could be a string, number, array (my language is weaktyped)

Variables occuring on the right side of the = are between "[" and "]".
When "[" and "]" appear on the left side of the =, it is similair to the variable_local_set function in gml.
« Last Edit: October 18, 2009, 07:49:06 pm by Somelauw » Logged

Bami
Jr. Member
**

Karma: 68
Offline Offline

Posts: 924


imageshack is ded?


View Profile WWW
« Reply #3 on: October 18, 2009, 08:08:44 pm »

It's supposed to print: 1, 1, 2, 3, 5, 8...
Well, your code does this.

print 1
print 1+0
print 1+0+0
print 1+0+0+0

if you would program it in your language it would be

Code:
Fibonacci
===========
def fib{n}
if {[n] <=1}
    return 1;
    end
return fib{[n]-1}+fib{[n]-2}
end

[i] = 1
while{1}
   print fib{i};
   i = % +1
   end

The code syntax is all too confusing though. Even perl has better syntax.
Logged
Somelauw
Newbie
*

Karma: 7
Offline Offline

Posts: 166


View Profile
« Reply #4 on: October 18, 2009, 09:04:15 pm »

Well, your code does this.

print 1
print 1+0
print 1+0+0
print 1+0+0+0
Wow, you are right. I forget changing the value of "b".

Quote
if you would program it in your language it would be

Code:
Fibonacci
===========
def fib{n}
if {[n] <=1}
    return 1;
    end
return fib{[n]-1}+fib{[n]-2}
end

[i] = 1
while{1}
   print fib{i};
   i = % +1
   end
Almost correct.
The if-statement should be like:
Code:
if [n] <= {1}
should be {i}

(When on the left side of =) would look up the value of i and use that value as a variable name. (kinda like a pointer in c)
Logged

SanMan
Jr. Member
**

Karma: 43
Offline Offline

Posts: 938


Kalau bisa mengerti maka PM saya.


View Profile
« Reply #5 on: October 18, 2009, 11:04:35 pm »

Wow, you are right. I forget changing the value of "b".

...and once again god showed us that it wasn't the language that was the problem...
Logged

I've started playing Eve again. May god have mercy on my soul.
Yourself
Sr. Member
****

Karma: 235
Offline Offline

Posts: 2002


The Ultimate Pronoun


View Profile WWW
« Reply #6 on: October 19, 2009, 07:12:07 am »

Python.

Now, I'm going to use the terribad recursive algorithm for generating the n'th Fibonacci number just to show off something cool: memoization.

Code:
def memoize_function(f):
    def func(*args):
        if args in func.memos:
            return func.memos[args]
        func.memos[args] = ret = f(*args)
        return ret
    func.memos = {}
    func.func_name = f.func_name
    return func

Now, whenever you want to memoize a function:

Code:
@memoize_function
def fib(n):
    if n < 2: return 1
    return fib(n - 1) + fib(n - 2)

Works on any function with any number of arguments.  I'm not sure about keyword arguments, though.  Basically what this does is caches function call results.  So, instead of the call stack for fib(5) looking like this:

fib(5)
  fib(4)
    fib(3)
      fib(2)
        fib(1) --> 1
        fib(0) --> 1
      --> 2
      fib(1) --> 1
    --> 3
    fib(2)
      fib(1) --> 1
      fib(0) --> 1
    --> 2
  --> 5
  fib(3)
    fib(2)
      fib(1) --> 1
      fib(0) --> 1
    --> 2
    fib(1) --> 1
  --> 3
--> 8


It will look like this:

fib(5)
  fib(4)
    fib(3)
      fib(2)
        fib(1) --> 1
        fib(0) --> 1
      --> 2
      fib(1) --> 1
    --> 3
    fib(2) --> 2
  --> 5
  fib(3) --> 3
--> 8


The improvement is even greater over larger numbers since the number of function calls in the standard algorithm grows exponentially with n (somewhat interestingly the number of function calls is equal to the answer that the function gives you).  Contrast this with the memoized version which grows linearly with n.

Of course, if you're working with exact arithmetic it's always better to go with an iterative approach:

Code:
def fib(n):
    a, b = 0, 1
    while n > 0:
        a, b = b, a + b
        n -= 1
    return b

Or if an approximation is good enough:

Code:
def fib2(n):
    n += 1
    sqrt5 = 5.0**0.5
    phi = (1.0 + sqrt5)/2.0
    return (phi**n - (-phi)**-n)/sqrt5

This will actually be exact to within rounding error.  So if you have a computer algebra system which stores exactly the square root of 5, you can compute exactly the Fibonacci numbers using this formula.
   

And just to stay on topic, the syntax of your language is needlessly arbitrary and confusing.  Use no superfluous symbol.
Logged

DarkFlame
Jr. Member
**

Karma: 18
Offline Offline

Posts: 626


Professional b*****d.


View Profile WWW
« Reply #7 on: October 19, 2009, 11:00:17 am »

Quote
Use no superfluous symbol.

+1
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!