Wednesday, September 7, 2022

Kamby – A programming language based on Lisp that doesn't seems like Lisp

Variables stack

Variables can contain any type of data, including number, strings, lists or even blocks of code.


message := 'Hello, World!'    # Define new variable (alternative: def key value)
message = 'Bye, bye'          # Edit variable value
def message 'Hello again!'    # Append new 'message' in stack (alternative syntax)
del message                   # Remove last 'message' in stack
message                       # Message has old value 'Bye, bye'
      

Variables are not unique. You can declare multiple values for the same variable name using ":=" and append in the stack. If you want to edit the last declaration, just user simple "=" operator. To remove the last variable with specific name in stack, use "del varname"

You can return the value of some stack item using index instead of var name


num1 = 12                     # If num1 was not previously declared, it'll be
num2 = 23
.                             # Return last item value from stack. 23
. 2                           # Return the second item from stack. 12
      

Expressions, lists and blocks

Basically those three types of lists has the same structure but act different when executed.


# Expressions will be evaluated an return the value
expression = (1 + 2)

# List items are separated by space. Expressions return will be attribuited to item
list = ['first item' (1 + 2)]

# Blocks are lists of expressions that can be called after declaration
block = {
  message := 'OK'
}
      

For source code or blocks, each line is considered an expression. To execute a block, just call it as first item of an expression. The arguments are be appended to the internal block variables stack


sum = { (. 1) + (. 2) }
# or
def sum { (. 1) + (. 2) }
sum 2 3
      

Context

Lists can be sent as a context appended to global variables context.


name := 'Global name'         # Declare a new variable in current stack

person = [
  name := 'Local name'        # The return of this attribution will be the list item.
  age := 20
]

name                          # Return 'Global name'

person :: { name }            # "person" is set as context to block execution.
                              # "name" returns 'Local name'
      

Using the context call, you can operate lists items like global variables


person :: { name = 'Local' }  # Edit "name" item of "person" list
person :: { del name }        # Remove named item of "person" list
person :: { del (. 1) }       # Remove item of "person" list by index
name                          # Return 'Global name'
      

Operators


+ - * /
&& || == != >= <= > <
+= -=
      

Operator "+" will sum two numbers, concatenate two strings, append a node to list or merge two lists.


1 + 2                    # 3
"Kamby" + "Lang"         # KambyLang
[1 2] += 3               # [1 2 3]
list = ([1 2] += [3 4])  # [1 2 3 4]
      

Behind the scenes

Internaly the implementation follows some basic concepts like S-expressions and car/cdr as any Lisp language.

Kamby has some conventions to make the syntax more friendly:

  • Anything starting with a new line and finishing in end of line is considered an expression
  • An item formed by 2 or less punctuation characters, will create an expression formed by (punct previous next). Ex.: 2 + 2 => (+ 2 2) ... something == anything => (== something anything)
  • Blocks will be evaluated if is the first item of expression.


from Hacker News https://kamby.org/

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.