Star Script

Simplify Web Application Development

Monthly Archives: November 2011

Introducing Star Script (7): Component and Tag

Enabling users to easily create and use component is an important design goal of Star Script. It is so important that Star Script has special language syntax to support it. The syntax that helps with component construction and usage is called tag, similar to the tag syntax in HTML/XML.

Tags, AKA markup, is considered “declarative” way of programming and easier for non-programmers to pickup. That’s why Star Script chooses tag to represent component. Integrating tags into a programming langauge allows user to avoid programming in certain use cases, such as UI declaration, content authoring, object value initialization, etc. When properly implemented, it can promote component oriented programming and improve the usability of a programming language.

Currently, in most programming languages, tags are not natively supported. Usually, they are treated as text and processed using language specific DOM libraries. There are a few languages that supports XML literal such as E4X, JavaFX, and Visual Basic. The functionality of the tag in those languages are quiet limited. Tags in those languages are mostly designed as a shorthand for creating XML fragment or constructing objects.

Star Script adopted an approach that treat tag as a first class citizen of the programming language. Below are some of the design points:

  • Easy tag definition: tag can be defined either by a function or a class. Any function can be called using tag syntax.
  • Tag has well-defined life cycle: tags go through four stages of their life cycle definition, instantiation, configuration, and rendering
  • Tag can be nested within other tags
  • Tag can be nested within other language structure
  • Tag can be assigned to variable or returned from function

Let use a simple example to illustrate some of the above points.

func row(n as int)
  <tr>
    <td>n<td><td>n * n<td>
  </tr>
end
<table>
  for i = 1 to 2
    <row n=i/>
  next
<table>

The output of above program is

<table><tr>
     <td>1<td>
     <td>1</td>
   </tr><tr>
     <td>2<td>
     <td>4</td>
   </tr></table>

As you can see from the example, for-statement can be embedded inside the table tag, and row tag can be part of the for-statement body. for-statement will execute and call the row function three times, each time with different parameter. The row function will render one row of the table, each time it is called.

Advertisements

Introducing Star Script (6): Basic Examples 3

Example: Condition

if 2 > 1 then
  hey, "2 > 1"
end
hey, 2 > 1

Example: Multiple Branch Condition

if 1 > 2 then
  Oops! "1 > 2" ???
elseif 2 > 1 then 
  right "2 > 1"
end
right 2 > 1

Example: Function

func sum(n as int)
  var s = 0
  for i = 1 to n
    s = s + i
  next
  return s
end
sum(10)
55

Introducing Star Script (5): Basic Examples 2

Example: Table

<table>
  for i = 1 to 3
    <tr>
       <td style="border:1px solid black;">i "*" 2 =</td> 
       <td style="border:1px solid black;">i*2</td> 
    </tr>
  next 
</table>

Output

1 * 2 = 2
2 * 2 = 4
3 * 2 = 6

Example: DoWhile Loop

var i = 1 
DoWhile i <= 3 
  we are at line i \n
  i = i + 1 
End

Output

we are at line 1 
we are at line 2 
we are at line 3

Introducing Star Script (4): Basic Examples 1

Let’s look at some simple Star Script examples

Example: Hello World!

Hello World!

Output

Hello World!

Example: Simple Expression

1 + 1

Output

2

Example: String Literal

"1 + 1"

Output

1 + 1

Example: Variable

var world = "my dear user"
Hello World!

Output

Hello my dear user!

Example: Assignment

var number as int
number = 1 + 1
number

Output

2

Example: Loop

for i = 1 to 3
  we are at line i \n
next

Output

we are at line 1
we are at line 2
we are at line 3

Introducing Star Script (3): Intuitive Syntax

Star Script targets casual programmers. Casual programmers don’t want to learn and remember too many things. Therefore, Star Script has a requirement that its syntax has to be intuitive. What does intuitive mean? It means for people who have never used Star Script before, when they look at a Star Script program for the first time, they can roughly guess what it means.

Based on this requirement, Star Script adopted the below syntax design:

  • No hard-to-guess symbols: Star Script does not use symbols like @ # $ % & in the syntax. I cannot guess what they mean in Perl, PHP, etc. I have to read tutorial first to understand it. Not only that, I always confuse its meaning when switching from one language to another. If I don’t use those languages for a while and try to pick them up later, I have to read tutorial again. This is not acceptable for a casual programmer. So those symbols are out. Star Script does use symbols such as +, -, *, / in the expression. They are considered intuitive for most people.
  • Do not require statement separator: many programming languages uses some kind of statement separator, such as semi colon, line break, etc. Statement separator is a concept you have to teach to get a user started using a language. To teach this concept, you have to introduce the concept of statement first. To teach the concept of statement, you have to show some examples such as if statement, for loop, etc. To make things clear, sometimes you have to teach the user how to distinguish between statement and expression, so they don’t put statement separator inside an expression accidentally. That is a lot of learning. So, it is out. Star Script will automatically separate statements. You can put them in one line or multiple lines without using semi colon. It’s all up to you. It is just formatting.
  • No general block enclosure: in C-like languages, you have curly braces “{” and “}” to indicate a statement block. In LISP, you have many many parentheses “(” and “)”. If you use those languages for a long time, you probably get used to it and no longer feels anything. If you are a casual programmer, those syntax is a major deterrence. It makes the language reads like formulas. And formulas are reserved for mathematically inclined people. For all other people, it just looks scary. So, it is out. You may wonder if casual programmer should at least be mathematically inclined. They may. But I don’t want to limit casual programmers to only mathematically inclined people. On the other hand, those enclosures can be designed away. This removes one concept people have to learn to get started. Why not?
  • Avoid inventing new keyword: a lot of casual programmers are actually expert in one or two programming languages. They are only casual for the other 100+ programming languages. We should leverage their existing knowledge to reduce learning curve if possible. So, I looked around and decided to borrow some keywords from C/Java/JavaScript, some from Basic. It makes users of those languages easier to get started. But there is no strict rule why Star Script picks this one and not that one. The picking is mainly based on taste.
  • Case insensitive: this is debatable. I personally like case-sensitive programming languages. It allows you to reuse the same word for different purposes. You just need to use different case. But, from my gut feeling and readings on the internet, it seems casual programmers like case-insensitive languages more, because natural languages are mostly case-insensitive. For natural language, cases are for formatting mostly. It does not change the meaning of the word. The most it does is to increase the volume of your SHOUT. So, case-insensitive is more intuitive, thus, Star Script is case-insensitive.
  • Intuitive formatting preservation: formatting characters will be ignored within a statement structure. But it should be preserved when you are generating output. Star Script tries to handle formatting as intuitive as possible. The goal is, when you feel a formatting character should be ignored, it should be ignored. When you feel a formatting text should be preserved, it should be preserved. It is a very high bar. I don’t think Star Script get it 100% right. But I hope it is close.
  • Multi-line string literal: I am not sure why major programming languages does not support multi-line string literal. For languages that uses line return as statement separator, they have an excuse. For languages that use semi colon as statement separator, I don’t see any good reason not to have multi-line string literal. Multi-line string literal is very convenient if you are processing a very long piece of text, such as a template. Neither adding \r\n everywhere, nor breaking down one string into multiple strings are convenient. Star Script supports multi-line string literal.
  • Array index starts with 1: in C-style programming languages, array index always starts with 0. There are many technical benefits with this approach. However, it is not intuitive. Given somebody without much programming experience, I am sure, teaching them to start counting from 1 is easier than teaching them to start from 0. The major downside for starting index from 1 is that, for experienced programmers, their brains have already being fixed by the C-style languages and they may feel counting from 0 more intuitive. Sorry, that’s what I have to say to them.

Introducing Star Script (2): Hello World

What does Star Script look like? Let’s start with some simple examples. The first program that people learn in most programming language is the “Hello World” program. The requirement is quite simple: please output the text “Hello World” using this language. You can check out the list of “Hello World” programs at many websites such as this.

Now, how to write the “Hello World” program in Star Script? It is the simplest you can ever imagine. Just enter the text “Hello World!”. The Star Script compiler will compile it, execute it, and output “Hello World!”, exactly as what you entered. Surprisingly simple. This behavior is quite unusual for a programming langauge. Yet it is exactly how most templating language behaves. For any template, if there is no special patterns recognized, output will equal input. At this point, I would like to make a bold statement: anybody, without any learning, can write program in Star Script. It is a statement which I don’t think any other programming language* can make. Yet, this is a feature that is extremely important to the target users of Star Script – casual programmers. As a casual programmer,  you want to get started instantly, without any learning. As you grow to be more experienced, of course, you will need to learn programming language concepts and features. But for Star Script users, you can learn it lazily, only learn a feature when you actually need it. You don’t have to learn many concepts such as class, method, arguments, static, void, main, array, string literal, etc, in order to write the first “Hello World” program. In fact, as a Star Script user, you need to learn nothing to get started.

Now, you see the templating side of the Star Script. How about the programming language side of it? If I enter an expression, will it get evaluated? Of course. If you enter 1+1, you will get 2 as the output. What if I want to display the text 1+1, and do not want it to be evaluated? Star Script also supports string literal, which is basically some text enclosed in the double quote (“). If you enter the program “1+1”, you will get the text 1+1 as output instead of 2.

How about other common programming constructs? Star Script is a fully featured, object-oriented programming language, so it supports all major programming constructs. Just as a simple example, if you want to say hello to some specific people instead the entire world, you can use the below program:

var world = "my dear friend"
Hello World!

You probably can guess it’s output. It is

Hello my dear friend!

To conclude this post, let’s have a little quiz. How to write a program that print out itself in C? Now, how to write a program that print out itself in Star Script?

*  I only consider a langauge as programming langauge when it is Turing Complete. Thus, I don’t consider HTML as a programming langauge.

Introducing Star Script (1): Accept Arbitrary Input

Star Script is a fully featured, object-oriented programming language with templating langauge extensions. It is the world’s first dot star programming language. It can process arbitrary input program in a best effort manner and then execute it. Unlike any existing programming langauge compilers, Star Script compiler does not reject any input program with compilation errors.

Wait. Isn’t compiler suppose to catch as many errors as possible so the user don’t get into problems later? That is very common thinking. But that is not good enough. Simply detecting error and rejecting the input is not very user-friendly. Imagine living in a world that requires everything to be 100% correct. When you try to turn on your car, you get “Errors! CD player not responding. Small crack detected on your windshield. Please fix them and try turning on the car again”. How would you feel? I would be really annoyed. Engine, transmission, everything else is working. Please let me turn on the car. I will fix the problems later.

Star Script compiler detects all the syntax and semantic errors. If there is any error, instead of simply rejecting the input and refuse to run. Star Script compiler goes a step further, it tries to recover from those errors. Error will still be reported to the end-user if they want, but the program won’t refuse to run. When you get a true engine failure kind of error, Star Script program will still run, just with empty output.

Star Script intends to be a programming langauge that casual programmers can use. Casual means you don’t need to learn much to get started. If you leave it for a while and want to pick it up later, it shouldn’t take much effort. To meet this goal, taking arbitrary input is an absolute requirement. This is because, without any training, people can only produce arbitrary input. Thus, arbitrary input is what Star Script has to accept.

Dot Star Programming Langauge

If you want to merge a templating language with a programing language, there are basically two approaches – either you extend a templating language with programming construct, or extend programming language with templating features.

So far, all templating systems uses the first approach. For example, for JSP, you have JSTL, which contains various tags, such as if, forEach, etc. This approach is easier to implement and the end result is not very desirable. For a list of pain points, please refer to the post Calling for a New Templating Language. In short, you end up with a language that is awkward and very limiting. This result is not completely unexpected. Why? If you compare the feature and size between a templating language and a programming langauge, you will find that, a programming language is usually much more powerful and sophisticated than a templating langauge. Templating langauge is much much smaller. To extend something really small to include features that only a much larger system can properly support, there is no surprise you will end up with something awkward and limiting.

Option two requires extending templating features on top of a programming language. It is harder and nobody has successfully done it. Nevertheless, it is the approach that actually makes sense. The reason is simple: extending something really small on top of something much larger is easier and could result in a much better result. This approach will lead to a type of programming language that I would call “Dot Star” programming languages. What does Dot Star mean? If you are familiar with the parsing technology, you will recognize that “.*” is a pattern that matches any input. Dot Star programming language is a programming language which accepts arbitrary input program. It is dramatically different from all of the existing programming languages*, yet it can be done as an extension to the existing programming language technology.

Star Script is an experiment of the second approach. The end result is a simple and powerful templating programming language.

* I only consider a langauge as programming langauge when it is Turing Complete. Thus, I don’t consider HTML as a programming langauge.