chronium / aatbe-lang

Åtbe language compiler. Written in Rust.
MIT License
5 stars 0 forks source link

Symbol literals #23

Open chronium opened 4 years ago

chronium commented 4 years ago

Add symbol literals for use with quoting and macro creation, dictionary keys and other constructs. Symbols are primitive values, and can be composed. They would also be useful for defining custom operators. For example, overloading the + operator could be the form of: fn :+ (self, other)

Symbol literals would have the form of: :[\S]+

Example

Given:

val sym = :test
val theMap = Map(:test => "Hello", :testcomp => "Hello World") // decide on key-value pair syntax

Extract the value:

// straight symbol argument
val hello = theMap :test
val hello2 = theMap(:test)

// compose symbol
val composed = sym :: :comp // creates symbol :testcomp

// symbol stored in variable
val helloWorld = theMap composed
val helloWorld2 = theMap(composed)
SplittyDev commented 4 years ago

Great idea, I loved this stuff in Ruby.

One thing that seems a bit weird to me is the operator overloads. So you'd use :+ to overload the + operator, but use it as simply + later?

In that case I think simply fn + (self, other) would make more sense.

The parser can easily handle that special case after fn so you don't have to allow it for all identifiers at once.

chronium commented 4 years ago

Fair enough with operators being simple, instead of symbols. But maybe we could allow symbol function names for manual dynamic dispatch for example.

Another thing, what would we do for postfix operators? With the current design ideas, prefix functions are any function that has a single parameter. Infix functions are basically any function that takes two parameters, but what about postfix? Maybe we could use symbol function names for postfix?

Example:


fn foo_self (self) // names can be symbols and literal symbols from now on as well

@prec(22) // specify precedence level
fn foo_inf (self, other)

fn :? (self) // defines postfix `?`

// usage
foo_self self_inst
self_inst.foo_self

self_inst foo_inf other_inst
self_inst.foo_inf other_inst
self_inst.foo_inf(other_inst)

self_inst? // calls postfix `?` on self
SplittyDev commented 4 years ago

If :? denotes postfix, how would you do prefix operators? Would it be ?: then for ?self_inst?

SplittyDev commented 4 years ago

Maybe you could use the : symbol to differentiate operator placement.

fn ? (self, true, false): foo.? (a, b) fn ?: self: ?foo fn :? self: foo?

chronium commented 4 years ago

In the current design, prefix is any function that takes a single argument.