MARGARET
An object oriented language system based on ruby and smalltalk.
Margaret is a modern programming language focused around objects and
messages. It offers powerful literals, easy to use objects and
smalltalk-like message structures.
Every value in Margaret is treated as an object.
(1 + 1) print,
comment: "`1` is an object, `+` is a message",
comment: "comment: is a method to the $Margaret object (yes it
was intentional)",
There are no reserved words and no explicit syntax apart from message sends.
$TrueProto = $Margaret clone,
$true = $TrueProto clone, $TrueProto -- [
#& a_boolean => a_boolean,
#and: alternate_block => alternate_block
value,
#as_bit => 1,
#if_false: alternate_block => $nil,
#if_false: false_alternate_block if_true:
true_alternate_block => true_alternate_block value,
#if_true: alternate_block => alternate_block
value,
#if_true: true_alternate_block if_false:
false_alternate_block => true_alternate_block value,
#not => $false,
#or: alternate_block => @self,
#xor: a_block => a_block value if_true:
$false if_false: $true,
#| a_boolean => $true,
],
$true freeze!,
comment: "That defines `$true` and inserts it into the object
system".
comment: "You guessed it, there exists a similar one for `false`".
Uses prototypal inheritance based on Self or JavaScript.
{ argc, argv |
$Point = $Margaret clone,
$Point attr_reader: [:x, :y],
$Point bind:
#x: xparam y:
yparam => {
@x
= xparam,
@y
= yparam,
@self
},
},
comment: "We bind new methods to existing objects",
Uses C-style literals like int, float, string.
2 + 3 print,
4.2 * 0.1 / 0.01 print,
res = "concat" + "me" + "to" + "something" print,
res get_first_character print,
Implements tensors, tuples, tables and bitstrings as array-like data structures.
['a', "a", 1, 1.0, %{ "k1": 42, k2: 43 }] each:
{ elem |
elem print
}
Runs on a portable, lightweight, embeddable, register based VM inspired by Lua.
$Margaret -- #ultimate_answer: x => {
$true && !false not not && ($nil is_nil?)
if_false: { exit: 0 },
y = @self methods size + @super to_string
length * 42,
[:symbol, ::label, 42, 42.2, "str", 0b0110,
0xbeef, 0o741, %[y, 42, "val"],
%{"k1": 42, k2: 43},
%(42, 1::1, 0::1)] each: { elem | (
puts: elem object_id,
},
if: x < y then: { x }
else: { y },
puts: (ultimate_answer: 42),
Technologies used
................................................................................................................................................................................................
MAIN LANGUAGE
................................................................................................................................................................................................
TESTING
................................................................................................................................................................................................
MEMORY MANAGEMENT
................................................................................................................................................................................................
TERMINAL EMULATION
Is this simply nu-smalltalk ?
Smalltalk was a good starting point for a pure object oriented language.
Having the benefit of hindsight and more computing power than even, we can
try revisit these old patterns, enriched with new techniques like modern
primitive literals and functional programming ideas. We can use fast and
efficient virtual machines and interpreters, as well as better memory
management techinques, to create a platform for experimentation in
human-centric applications.
Why is margaret written in C ?
It might make sense to code a new interpreter in the highest level
language possible, and initially it was designed in ruby, but was quickly
refactored in C. The main reason for such a choice was the fact that
margaret is a virtual machine based language. Considering that there is
intermediate bytecode that is actually being executed, and taking into
account the need for dynamic modification of internal object structures
and their messages, the interpreter that executes the bytecodes must be as
efficient as possible. C is a solid choice not only for speed but for
memory efficiency as well. The fact that the language is not a pure
compiler draws certain limits in the speed of its execution. Most likely
margaret can never have an efficient self bootstrap, and will depend on C
(or some other low level fast language) for a long time.
Modern data structures.
As a language of the 2020s, margaret strives to be the first step in a new
wave of languages, focused on the needs of today. Any language of the 90s
had a flair of the era of scripting, faster to MVP languages, ones like
perl, python or ruby. There was an obvious need for easy to use data
structures, that were dynamic since most of the software of that decade,
especially with the emergence of web technologies, required fast paced
teams that could get a product out in the market as fast as possible. 30
years later we have started to move away from simple software and instead
looked at developing more and more complicated systems. The tools we have
however are not good enough. The needs of today require massive
collaboration between software systems, as well as newer and more
efficient data management solutions. Margaret utilizes object based
literals for tensors (with a heavy-loaded API for tensor operations) as
well as efficient hashtables.