Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specification improvements #40

Merged
merged 8 commits into from
May 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ before_script:
# Add LLVM shared library
- "export LD_LIBRARY_PATH=/usr/lib/llvm-3.5/lib:$LD_LIBRARY_PATH"
# Setup library for development building
- "node_modules/.bin/jake default ts:compile"
- "npm run grammar"
- "npm run gen-spec"
- "node_modules/.bin/jake all"
script:
- "npm run test"
- "npm run test-spec"
- "npm run test-llvm"
- "node_modules/.bin/mocha test"
- "node_modules/.bin/mocha test/spec"
- "node_modules/.bin/mocha test/llvm"
after_script:
- "npm run report-coverage"

80 changes: 73 additions & 7 deletions Jakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs'),
path = require('path'),
util = require('util'),
child_process = require('child_process'),
glob = require('glob'),
Expand All @@ -9,19 +10,86 @@ var paths = {
}

function exec (cmd, opts) {
console.log(cmd)
child_process.execSync(cmd)
// console.log(cmd)
var result = child_process.execSync(cmd)
if (result.length > 0) {
console.log(result.toString().trim())
}
}

function formatSeconds (duration) {
var totalSeconds = duration / 1000;
return Math.round(totalSeconds * 100) / 100
}

desc('Build the standard library')
file('lib/std.o', ['ext/std.c'], function () {
var outfile = this.name,
var start = new Date(),
outfile = this.name,
infile = this.prereqs[0]
exec("clang -c "+infile+" -o "+outfile)
console.log('Native library compiled in '+chalk.magenta(formatSeconds(new Date() - start)+' s'))
})

desc('Default building actions')
task('default', ['lib/std.o'])
task('default', ['lib/std.o', 'grammar', 'ts:compile'])

desc('Compile everything possible')
task('all', ['default', 'specification'])


// Grammar -------------------------------------------------------------------

desc('Build parser from grammar')
task('grammar', ['src/grammar.js'])

file('src/grammar.js', ['src/grammar.pegjs'], function () {
var start = new Date(),
infile = this.prereqs[0]
exec('node_modules/.bin/pegjs --cache '+infile)
console.log('Grammar generated in '+chalk.magenta(formatSeconds(new Date() - start)+' s'))
})


// Specification -------------------------------------------------------------

desc('Generate specification tests')
task('specification', function () {
var start = new Date(),
parseSpec = require('./src/spec-parser').parseSpecification
specSource = fs.readFileSync(__dirname+'/doc/specification.md').toString(),
runnerSource = fs.readFileSync(__dirname+'/share/spec-runner.js').toString(),
specs = parseSpec(specSource),
specTestDir = __dirname+'/test/spec'

// Remove old specification files
var files = fs.readdirSync(specTestDir),
removed = 0
for (var i = 0; i < files.length; i++) {
var f = path.join(specTestDir, files[i])
if (!/\.js$/.test(f) && !/\.hb$/.test(f)) { continue }
fs.unlinkSync(f)
removed += 1
}
console.log('Removed '+chalk.magenta(removed)+' old specification files')

// Now generate the specs
for (var i = specs.length - 1; i >= 0; i--) {
var spec = specs[i]

var js = spec.js+"\n",
hb = spec.hb+"\n"
fs.writeFileSync(specTestDir+'/source-'+spec.name+'.js', js)
fs.writeFileSync(specTestDir+'/source-'+spec.name+'.hb', hb)

var runner = runnerSource.replace(/NAME/g, spec.name)
fs.writeFileSync(specTestDir+'/test-'+spec.name+'.js', runner)

console.log("Generated tests for '"+chalk.cyan(spec.name)+"'")
}
console.log('Specification tests generated in '+chalk.magenta(formatSeconds(new Date() - start)+' s'))
})


// TypeScript ----------------------------------------------------------------

Expand Down Expand Up @@ -82,9 +150,7 @@ namespace('ts', function () {
console.log("Failed to compile file '"+chalk.red(fileName)+"'")
}
})
var totalSeconds = (new Date() - compileStart) / 1000,
formattedSeconds = Math.round(totalSeconds * 100) / 100;
console.log('Finished in '+chalk.magenta(formattedSeconds+' s'))
console.log('Finished in '+chalk.magenta(formatSeconds(new Date() - compileStart)+' s'))
})

desc('Watch for changes')
Expand Down
39 changes: 39 additions & 0 deletions doc/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,42 @@ while (a < 10) {
}
```
</spec>

## For

<spec name="for">
```hb
var b = 0
for var a = 1; a < 4; a += 1 {
b = b + a
}
# b will equal 6 here
```
```js
var b = 0;
for (var a = 1; a < 4; a += 1) {
b = b + a;
}
```
</spec>

## If

<spec name="if">
```hb
var a = 1
var b = 0
if a {
b = 1
}
# b will equal 1 here
```
```js
var a = 1;
var b = 0;
if (a) {
b = 1;
}
```
</spec>

30 changes: 0 additions & 30 deletions share/gen-spec.js

This file was deleted.

11 changes: 6 additions & 5 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ function compileFile (args) {
return file
}

// Load the JavaScript compile target and print the compiled source
var javascript = require('./targets/javascript'),
jsCompiler = new javascript.JSCompiler()

var commands = {
inspect: function (args) {
var file = compileFile(args)
Expand All @@ -133,10 +137,7 @@ var commands = {
if (file.tree.imports.length === 0) {
targetOpts.single = true
}
// Load the JavaScript compile target and print the compiled source
var javascript = require('./targets/javascript'),
compiler = new javascript.JSCompiler(),
compiled = compiler.compileRoot(file.tree, targetOpts)
var compiled = jsCompiler.compileRoot(file.tree, targetOpts)
process.stdout.write(compiled)
// Check whether we should also print the source-map
var includeMap = argv.map
Expand All @@ -152,7 +153,7 @@ var commands = {
// Load the vm module and JavaScript target compiler
var vm = require('vm')
// Compile the whole file into a bundle to run
var compiledSource = file.tree.compile()
var compiledSource = jsCompiler.compileRoot(file.tree, targetOpts)
// Expose "require(...)" to the script
global.require = require;
// Run the compiled source in the VM
Expand Down