Skip to content

Commit

Permalink
Introduce SemiScript.
Browse files Browse the repository at this point in the history
* Allows us to support popular languages, now, and far into the future.
* Translates to JavaScript and CoffeeScript, but is modular enough to
  easily be extended to support other languages.
  • Loading branch information
alloy committed Apr 15, 2012
1 parent 3977be3 commit f9a916a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.o
.*.sw?
semiscriptc
distbuild
9 changes: 9 additions & 0 deletions Makefile
@@ -0,0 +1,9 @@
semiscriptc: compiler.o
gcc compiler.c -o semiscriptc

distbuild: semiscriptc
mkdir -p distbuild/JS && ./semiscriptc -js semicolon.semi > distbuild/JS/semicolon.js
mkdir -p distbuild/CoffeeScript && ./semiscriptc -cs semicolon.semi > distbuild/CoffeeScript/semicolon.coffee

clean:
rm -rf semiscriptc *.o distbuild
46 changes: 45 additions & 1 deletion README.md
Expand Up @@ -3,6 +3,49 @@
Semicolon.js is a much more secure, stable and reliable alternative to
<a href="http://vaporjs.com/">Vapor.js</a>.


## Build:

After much discussion, it became clear that to support this library far into the
future, we would need to abstract the requirements into its own language, which
can be compiled down to different languages.

Currently the compiler generates valid code for JavaScript and CoffeeScript, but
could easily be extended to support additional languages.

The compiler can be build with:

```
make
```

It produces the `semiscriptc` tool, which can then be used to compile SemiScript
source. These source files typically have the `.semi` extension.

JavaScript:

```
./semiscriptc -js semicolon.semi
;
```

CoffeeScript:

```
./semiscriptc -cs semicolon.semi
```

Beatiful.

Finally, a convenience task is available to produce products for both JavaScript
_and_ CoffeeScript:

```
make distbuild
```


## Usage:
```html
<script src="semicolon.js"></script>
Expand All @@ -20,6 +63,7 @@ Thanks to @alloy for pointing out the inherent code security and
interoperability problems with Vapor.js; and suggesting to
leverage the semicolon solution to address the underlying issues.


### License

(c) Copyright 2012 Thomas Fuchs
Expand All @@ -35,4 +79,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.
60 changes: 60 additions & 0 deletions compiler.c
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[])
{
if (argc != 3) {
printf("Usage: %s [-js|-cs] FILE\n", argv[0]);
return 1;
}

unsigned char javascript = 0;
if (strcmp(argv[1], "-js") == 0) {
javascript = 1;
}

char *source_file = argv[2];

FILE *file = fopen(source_file, "r");
if (file != NULL) {
unsigned char comment = 0;
while (1) {
char c = fgetc(file);
if (c == EOF) {
break;
}
switch (c) {
// This is where the language translation magic happens.
case ';':
if (!comment) {
if (javascript) {
// JavaScript translation.
putc(';', stdout);
} else {
// CoffeeScript translation.
}
}
break;

// Pass-through new-lines, unless in a comment.
case '\n':
if (!comment) {
putc('\n', stdout);
}
comment = 0;
break;

// Comment, ignore the rest of the line.
case '#':
comment = 1;
break;
}
}
fclose(file);
} else {
printf("Unable to read semiscript source file `%s'.\n", source_file);
return 1;
}

return 0;
}
1 change: 0 additions & 1 deletion semicolon.js

This file was deleted.

2 changes: 2 additions & 0 deletions semicolon.semi
@@ -0,0 +1,2 @@
# This source file is available under the GPL license.
;

0 comments on commit f9a916a

Please sign in to comment.