<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hatsuseno&#039;s blog &#187; syntax</title>
	<atom:link href="http://blog.hatsuseno.org/index.php/tag/syntax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hatsuseno.org</link>
	<description>rantings on tech</description>
	<lastBuildDate>Tue, 03 Aug 2010 12:58:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>building a programming language, part 2</title>
		<link>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-2/</link>
		<comments>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-2/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 13:02:30 +0000</pubDate>
		<dc:creator>hatsuseno</dc:creator>
				<category><![CDATA['cwx' project]]></category>
		<category><![CDATA[cwx]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://blog.hatsuseno.org/?p=90</guid>
		<description><![CDATA[Following up on my original post, this time, version 1.0 &#8220;final&#8221; for the basic structure and syntax of the language, still unnamed, although someone suggested I should integrate &#8216;two&#8217; and &#8216;hash&#8217; in there. Oh well.
The spirit remains, but there have been a few changes in the language as a whole. The entire codeblock-as-type feature has [...]]]></description>
			<content:encoded><![CDATA[<p>Following up on my original post, this time, version 1.0 &#8220;final&#8221; for the basic structure and syntax of the language, still unnamed, although someone suggested I should integrate &#8216;two&#8217; and &#8216;hash&#8217; in there. Oh well.</p>
<p>The spirit remains, but there have been a few changes in the language as a whole. The entire codeblock-as-type feature has been dropped until it can be further perfected syntactically, and I&#8217;ve taken a more pragmatic approach in general, things need to get done after all.</p>
<p><h2>Built-in types</h2>
<dl>
<dt><code>boolean</code></dt>
<dd>A boolean type, true or false.</dd>
<p></p>
<dt><code>integer</code></dt>
<dd>Pretty obvious, integer numbers, 64bit signed.</dd>
<p></p>
<dt><code>float</code></dt>
<dd>Less obvious, implemented as a C-style double.</dd>
<p></p>
<dt><code>string</code></dt>
<dd>A string type, most types can be cast to this.</dd>
<p></p>
<dt><code>list</code></dt>
<dd>Simple, LISP-style, linked-list.</dd>
<p></p>
<dt><code>array</code></dt>
<dd>Variable-length, numerically indexed arrays.</dd>
<p></p>
<dt><code>hash</code></dt>
<dd>Variable-length hashmap, /[[:alnum:]_]+?/ keys.</dd>
<p></p>
<dt><code>function</code></dt>
<dd>Standard first-class citizen functions.</dd>
<p></p>
<dt><code>class</code></dt>
<dd>First-class citizen classes.</dd>
<p></p>
<dt><code>dynamic</code></dt>
<dd>Special untyped variable, can contain *any* type value at any time, default type</dd>
<p>
</ul>
<h3>Examples of primitive types</h3>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">boolean b = true;
integer x = 5;
float j = 5.4E-4;
string s = 'test string';
list l = list(1, 2, 3, 4, 5);
array m = array('a', 'b', 'c', 'd', 'e');
hash z = hash(a: 1, b: 2, c: 3, d: 4, test_key: 666);
&nbsp;
function f = function(a, b, c, d) uses (m) {
  sum(a, b, c, d, m[1]);
};
&nbsp;
class c = class(
  private integer attr_name: 5,
  public function constructor myConstructor: function() {
    this.attr_name *= 10;
  }
);
dynamic y = 20;
p = 20;</pre></div></div>

<p>A few sidenotes;</p>
<ul>
<li>Keys in hash-maps are unquoted, because they are properly constrained.</li>
<li>Functions are <i>always</i> closures, but require explicit declaration of the enclosed variables.</li>
<li>Classes syntactically resemble hash-maps, but have modifier keywords, like private, protected, constructor and destructor.</li>
<li>The variables y and p are for all intents and purposes identical in content and type, untyped variables *are* dynamic variables.</li>
<li>All composite types (like lists, arrays and classes) have a constructor-function, which is an actual function that can be called with eval()-esque intent.</li>
</ul>
<p><h2>In-depth look into the composite types</h2>
<h3>Lists</h3>
<p>Lists are singly-linked lists, and the functions <code>list()</code>, <code>first()</code> and <code>rest()</code> are the primitive operators on lists.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">x = list(1, 2, 3, 4, 5);
first(x);
  =&gt; 1
rest(x);
  =&gt; list(3, 4, 5)</pre></div></div>

<p><code>first()</code> and <code>rest()</code> are modifying functions, they pop from the list whatever they return, so the list <code>x</code> is now a list with the value <code>(2)</code>. Trying to pop from an empty array results in a &#8216;null&#8217; value being returned. While-loops can be constructed around this, but lists are iteratable, so the built-in <code>foreach()</code> will do just fine.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">foreach(list(1, 2, 3, 4, 5) as li) { ... }</pre></div></div>

<h3>Arrays</h3>
<p>Arrays are numerically indexed, sparse datastructures. So you can have a value at index 1 and at 5, nothing stops you from breaking sequence. By default an array can contain values of any type (so it&#8217;s implicitly an array of dynamic values) but this behaviour can be changed easily.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">integer array a = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
a[9] = 10;
a[1] = null;
a;
  =&gt; array(1, 3, 4, 5, 6, 7, 8, 9, 10);
&nbsp;
a[1];
  =&gt; null;</pre></div></div>

<p>Array <code>a</code> is explicitly an array of integer values, and so cannot be assigned anything else. Doing so will result in a run-time error. (Type-checking is always done in run-time).<br />
Unsetting array indecis is done by setting the value to <code>null</code>, as you can see when evaluating the array, index 1 does not exist. Evaluating a non-existant index of an array produces <code>null</code> as well. There is no difference between non-existing indecis and values set to <code>null</code>.</p>
<h3>Hash-maps</h3>
<p>Similar to arrays in syntax to arrays, maps are variable in length, and implicitly of type &#8216;dynamic&#8217;. This can be changed in the same way it can for arrays.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">integer hash m = hash(a: 1, b: 2, c: 3, d: 4);
x = map(function (val, key) { ... }, m);</pre></div></div>

<p>Introducing the function <code>map</code>, although it could be easily constructed using lower-level operations, a high-order function provided by the language runtime.</p>
<h3>Functions</h3>
<p>Being first-class citizens, functions get assigned to variables, and nothing else. All functions are closures, but require a list of variables to enclose. This is to keep GC simpler and it requires the developer think about what he or she wants to enclose.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">square = function(x) { return x^2; } // f(x) = x^2;
&nbsp;
deriv = function(f, dx) {
  return function(x) uses (f, dx) {
    return (f(x + dx) - f(x)) / dx
  };
};
&nbsp;
d_square = deriv(square, 0.001); // Approximates f'(x) = 2x;</pre></div></div>

<p>Provided is an example demonstrating the use of this language in creating a function that produces a function approximating the derivative of another function.</p>
<h3>Classes</h3>
<p>Classes are a bit different from the usual OOP approach, and are halfway between normal OOP classes and prototype-based languages. Classes are first-class citizens, and can be modified in run-time. Immediately the usual implied trust that a class will remain the same during it&#8217;s entire lifetime no longer exists, although there are methods of regaining that trust.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">p = class(
  private integer x,
  private integer y,
&nbsp;
  public constructor function myConstructor: function() {
    this.x = 5; this.y = 5;
  },
&nbsp;
  public function toString() {
    return &quot;x: &quot; ~ x ~ &quot;, y: &quot; ~ y;
  }
);
&nbsp;
object = new p;
p.toString();
  =&gt; string &quot;x: 5, y: 5&quot;</pre></div></div>

<p>Looks reasonable enough, create a class, make an object, execute method, the usual. Inheritance is also mostly the same as PHP and Java classes.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">k = class() extends p; // Empty class extending class p.
&nbsp;
z = interface(public function toList);
t = class(
  public function toList: function() {
    return list(this.x, this.y);
  }
) extends p implements z;</pre></div></div>

<p><code>t</code> is a class implementing interface <code>z</code> and extending class <code>p</code>. It&#8217;s a bit ugly with the modifiers at the bottom, but it works fine. But because classes are also first-class citizens, and not the global structures we know from Java and C++ for example, you can hide classes in context, effectively using the code-scope mechanism to implement a system similar to namespaces in that it provides some classes can be directly accessed from the entire project, and some are internal to a function, a class or any other scope-seperating block.</p>
<p>The role/mixin/trait feature described in the first post about this programming language has been dropped until I get my head around it well enough to see the implications of adding it.
</p>
<p>
That about sums it up for the built-in types for version 1.0 of the language specification, though I&#8217;m sure there are flaws in here that need resolving before it can be implemented properly. Critisism, as always, is appreciated, save for spelling errors and such, I am already aware my English skill needs upgrading <img src='http://blog.hatsuseno.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  For now, I can start working on the grammar/syntacical parser of the interpreter. After that comes a symbol table and after that I&#8217;ll be sure to post an update here.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>building a programming language, part 1</title>
		<link>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-1/</link>
		<comments>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-1/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 11:27:32 +0000</pubDate>
		<dc:creator>hatsuseno</dc:creator>
				<category><![CDATA['cwx' project]]></category>
		<category><![CDATA[cwx]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://blog.hatsuseno.org/?p=48</guid>
		<description><![CDATA[As a follow-up from a school assignment (where I had to build a full parser for a very simple language) I decided to construct a language of my own, just for fun.
Orignally, I was a C programmer, my part-time job entails PHP, and I have a fondness for languages like Scheme, Lua and Javascript. Drawing [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow-up from a school assignment (where I had to build a full parser for a very simple language) I decided to construct a language of my own, just for fun.</p>
<p>Orignally, I was a C programmer, my part-time job entails PHP, and I have a fondness for languages like Scheme, Lua and Javascript. Drawing inspiration from all these language, I have (for now) decided on a syntax like this;</p>
<p><h2>Variables</h2>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">int x = 42;
dynamic j = 'x';
y = 'x';</pre></div></div>

<p><code>j</code> and <code>y</code> are both of the same type, &#8216;<code>dynamic</code>&#8216;, in the case of <code>y</code>, this is implied. Dynamic variables can contain any type of content, others are type strict.
</p>
<p><h2>Built-in types</h2>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">int x = 42;
float j = 4.5;
string s = &quot;hello world&quot;;
array l = (1, 2, 3, 4, 'a', 'b', 'c', 'd');
map m = [a: 1, b: 2, c: 3, d: 4];
code c = { a = 5; a++; doSomething(a); };
function f = function() {};
class c = class [];</pre></div></div>

<p>Statements are first class citizens of the language, albeit not for the right reasons yet.
</p>
<p><h2>Functions</h2>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">string s = &quot;Amount &quot;;
&nbsp;
f = function(int x, function y, z) uses (s) {
  x += y(z);
  return s ~ x;
}</pre></div></div>

<p>This here example shows literal strings as a language construct, and first class citizenship of functions. All functions are closures, but require (<a href="http://wiki.php.net/rfc/closures">like</a> in PHP) the desired variables in the closure to be explicitly summed up. String concatenation is done through the &#8216;<code>~</code>&#8216; operator.
</p>
<p><h2>Classes</h2>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">surface = class [
  private x : 0,
  private int y,
&nbsp;
  public constructor makeNew : function {
    this-&gt;x = 5;
    this-&gt;y = 7;
  },
]</pre></div></div>

<p>Classes, in my opinion, are more like hash-tables than functions, as such, the syntax reflects this. &#8216;<code>constructor</code>&#8216; is a special keyword that indicates that the method defined is the constructor of said object. As shown, the name of this method can be anything. I haven&#8217;t quite decided on overloading and it&#8217;s implications, so I&#8217;ve left it at that. As with functions, classes are first class citizens of the language. That means they can be assigned to variables, copied and have operations performed on them. An example:</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">// Class with two attributes, implied public, of undefined type
a = class [ x, y ];
&nbsp;
// 'b' is an empty class that extends 'a'
b = class [] extends a; 
&nbsp;
c = interface [ public int x, public function getName ];
d = class []; // 'd' is an empty class
d implements c; // which now implements 'c'</pre></div></div>

<p>Operators like &#8216;<code>extends</code>&#8216; and &#8216;<code>implements</code>&#8216; can be applied to a class during it&#8217;s entire lifetime. Instances of those classes made before the operations do not change with them.</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">/* Classes can be extended with anonymous classes,
 * same for interfaces.
 * Not really useful, but syntactically it's valid
 * sidenote: newlines are regarded as whitespace */
a = class [ x, y ] extends class [ 
     function getName() { return 'name'; }, int c ]
     implements interface [ function getType ];
&nbsp;
x = class [ function getThing(a, b) { return a + b; } ];
&nbsp;
a has x;     // 'a' now has a method 'getThing'
b extends a; // but 'b' does not.</pre></div></div>

<p>A trick I picked up from Perl 6, class-traits. I believe this will be very beneficial in bridging traditional classes and prototype-based languages by allowing class extending without adding to the inheritance chain.
</p>
<p><h2>Operators</h2>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">/* All common symbolic operators (+, -, &amp;&amp;, extends) have
 * function-like counterparts. Internally, the symbolic ops
 * are just syntactic sugar for the function-like ops */
print(2 + 2);
print(add(2, 2));
&nbsp;
print(true &amp;&amp; false);
print(and(true, false));
&nbsp;
// Variable amount of parameters when it makes sense
print(and(true, true, true, true, true, true, false));
print(subtract(40, 3, 4, 6, 2, 13));</pre></div></div>

<p>But also</p>

<div class="wp_syntax"><div class="code"><pre class="cwx" style="font-family:monospace;">/* Symbol quoting stolen from Lisp,
 * symbols a, b and c are not evaluated */
function a = _function(('a, 'b, 'c), (x, y, z), {
  return add(a, b, c) * subtract(x, y, z); });</pre></div></div>

<p>Which, as you can see is exceedingly ugly. Neither concise nor elegant, the syntax has grown to represent what I want to avoid in general. This either means I will need to get a clue as to resolving this syntactically, or I will have to remove the operators-as-functions feature from the language, but I haven&#8217;t decided. The entire concept of code as a first class citizen of the language (like in Smalltalk) was created out of necessity to make this work.
</p>
<p><h2>Conclusion (for now, anyway)</h2>
<p>Although I have started with a parser for the basic language, stuff like the operators-as-functions and first class citizenship of codeblocks need to be resolved to add to the whole of the language instead of looking like I just clobbered it all together. Also, I still need to name it&#8230; &#8216;project cwx&#8217; sounds increasingly cheezy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hatsuseno.org/index.php/building-a-programming-language-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
