From 665fa2bf002514750fd63cf91efe744eeb16fa8d Mon Sep 17 00:00:00 2001 From: Dre_ Date: Wed, 31 May 2017 21:10:53 +0000 Subject: [PATCH 1/6] pushing the code for the compiler.js file --- app.js => compiler.js | 203 +++++++++++++++++++++--------------------- index.html | 50 ----------- main.css | 54 ----------- 3 files changed, 100 insertions(+), 207 deletions(-) rename app.js => compiler.js (60%) mode change 100644 => 100755 delete mode 100644 index.html delete mode 100644 main.css diff --git a/app.js b/compiler.js old mode 100644 new mode 100755 similarity index 60% rename from app.js rename to compiler.js index db6368c..fc083c8 --- a/app.js +++ b/compiler.js @@ -1,22 +1,8 @@ +#!/usr/bin/env node -var editor = ace.edit("editor"); // the numbering -editor.setTheme("ace/theme/monokai"); // theme -editor.getSession().setMode("ace/mode/javascript"); // language want to use -editor.setValue("# Start your work here\n"); // adding a value -editor.session.setOption("useWorker", false); //disable the corrections +var fs = require('fs'); - -var editor2 = ace.edit("editor2"); // the numbering -editor2.setTheme("ace/theme/chrome"); // theme -editor2.getSession().setMode("ace/mode/javascript"); // language want to use -editor2.setReadOnly(true); // make the editor only read -editor2.setValue("// Javascript will appear here\n"); -editor2.session.setOption("useWorker", false); - - - - -var data = { +var global_object = { variables: [], func: [], wordGist: [], @@ -38,10 +24,12 @@ function includes( array, item ) { function run() { try { - eval( data.code ); + + eval( global_object.code ); } catch(e) { - console.log('*error: ' + e.message); + + catchError('wordScript *error: ' + e.message); } } @@ -52,33 +40,34 @@ function print( input ) { if ( input.length === 1 ) { // return back the translated javascript code - return 'console.log('+ input.join(' ') +');'; + return `console.log( ${ input.join(' ') } );`; } /* checks with the input is a variable already declared in the data obj variables property */ - for ( var i in data.variables ) { + for ( var i in global_object.variables ) { /* if it exsit then return back the stringifyed sjavascript code with the value of the concated inside of the string */ - if ( data.variables[i].name === input ) { + if ( includes( input, global_object.variables[i].name ) ) { - return 'console.log('+ data.variables[i].value +');'; + return `console.log( ${ input.join(' ') } );`; } } if ( input[0] === 'call' ) { var func = callFunction( input ); - // console.log( func.slice(0, func.length - 1) ); - return 'console.log( ' + func.slice(0, func.length - 1) + ' );'; + + return `console.log( ${ func.slice(0, func.length - 1) } );`; } + return `console.log( ${ input.join(' ') } );` } @@ -90,12 +79,12 @@ function print( input ) { // ["x", "y", "z"] function makeFunction( input ) { - data.func = []; + global_object.func = []; var func_name = input[2]; - data.func.push({ + global_object.func.push({ func_name: input[2], params: input[3][0] !== '[' ? null : input[3].slice(1, input[3].length - 1).split(','), body: input.length === 7 ? input.splice(3, input.length).join(' ') : input.splice(4, input.length).join(' ') @@ -108,16 +97,16 @@ function makeFunction( input ) { // arg.slice(1, arg.length - 1).split(',') // ["x", "y", "z"] - for(var i = 0; i < data.func.length; i++) { + for(var i = 0; i < global_object.func.length; i++) { - if (data.func[i].func_name === func_name ){ + if (global_object.func[i].func_name === func_name ){ - var body = data.func[i].body.split(' '); + var body = global_object.func[i].body.split(' '); if ( body[0] !== 'return' && body[0] !== 'print' ) { - body = 'return ' + body.join(' ') + ';'; + body = `return ${ body.join(' ') } ;`; } else if ( body[0] === 'print' ) { @@ -125,11 +114,11 @@ function makeFunction( input ) { } else { - body = body.join(' ') +';'; + body = `${ body.join(' ') } ;`; } - return "function " + data.func[i].func_name +"("+ data.func[i].params +") { \n \t" + body +" \n}"; + return `function ${ global_object.func[i].func_name} ( ${ global_object.func[i].params } ) { \n \t ${ body } \n}`; } } @@ -138,19 +127,19 @@ function makeFunction( input ) { function callFunction( input ) { - for(var i = 0; i < data.func.length; i++) { + for(var i = 0; i < global_object.func.length; i++) { - if (data.func[i].func_name === input[1]){ + if (global_object.func[i].func_name === input[1]){ - for ( var i in data.variables ) { + for ( var i in global_object.variables ) { - if ( input[input.length - 1] === data.variables[i] ) { + if ( input[input.length - 1] === global_object.variables[i] ) { - return input[1] + '('+ data.variables[i] +');'; + return `${ input[1] } ( ${ global_object.variables[i] } );`; } } - return input[1] + '('+ input[input.length - 1] +');'; + return `${ input[1] } ( ${ input[input.length - 1] } );`; } } @@ -191,14 +180,14 @@ function makeVariable( input ) { and push them to the data obj to variable property. */ - data.variables.push({ + global_object.variables.push({ name: input[0], value: input[1], }); // lastly return the javascript code of the fromated variable - return 'var ' + input[0] + ' = ' + input[1] + ';' ; + return `var ${ input[0] } = ${ input[1] };`; } /* @@ -216,43 +205,74 @@ function makeVariable( input ) { to the data obj and tore them in the variables property */ - data.variables.push({ + global_object.variables.push({ name: input[0].split(' ').join(''), value: input[1].split(' ').join('') }); // lastly return the javascript code of the fromated variable - return 'var ' + input[0].split(' ').join('') + ' = ' + input[1].split(' ').join('') + ';'; + return `var ${ input[0].split(' ').join('') } = ${ input[1].split(' ').join('') };`; } // saves the code that the user word in wordScript -function makeWordGist() { +/*function makeWordGist() { + - /* pushes the code to the data obj to property wordGist - */ + data.wordGist.push({ name: prompt('name'), code: editor.getValue(), translated_code: data.code }); -} +}*/ // makes a loop function makeLoop( input ) { // gets the input of the frist and last offset - var start = input[2]; - var end = input[input.length - 1]; + + + if ( includes(input, ':') ) { + + if ( includes(input, 'print') ) { + + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ print( input.splice( input.indexOf('print'), input.length ) ) } \n}`; + } + else { + + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ input.splice(input.indexOf(':') + 1, input.length).join(' ') } \n}`; + } + } // return the translated javascript code - return 'for ( var i = '+ start +'; i <= '+ end +'; i++ ) {\n console.log( i );\n}' + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n console.log( i ); \n}`; } + + +function catchError( err, callback ) { + + + if ( callback === undefined ) { + + console.log( err || `*error: ${ err.message ? err.message : 'error returned ' + err }` ); + return; + } + else if ( callback !== undefined ) { + + return callback({ + status: false, + msg: err || `*error: ${ err.message.split(' ').splice(1, err.message.split(' ').length).join(' ') }` + }, undefined); + } +} + + // parsers the worScrit code function parseInput( input ) { @@ -304,60 +324,37 @@ function parseInput( input ) { return output; } +var arg = process.argv.splice(process.argv.length - 1, process.argv.length); - - -$(document).ready(function(){ - $("#editor").keydown(function(key){ - if(key.which === 13){ +if ( arg.length !== 0 ) { + + fs.exists(`${ __dirname }/${ arg }`, ( err, rsp ) => { + if ( !err ) return catchError( err ); + + fs.readFile(arg[0], 'utf8', ( err, data ) => { + if ( err ) return catchError( err ); - var input = editor.getValue().split('\n'); + var script = data.split('\n'); - data.code = ''; - for(var i in input){ - data.code += parseInput( input[i] ) + '\n'; + if ( script[0] === '' ) { - if ( data.code !== undefined ) { - editor2.setValue( data.code ); - } - } - } - }); - - $('#run').click(function() { - run(); - }); - - - $('#save').click(function() { - makeWordGist(); - }); - $("#print").click(function(){ - - editor.insert("\nprint "); - }); - $("#function").click(function(){ - editor.insert("\nmake function "); - }); - $("#parameter").click(function(){ - - editor.insert("\n[ ]"); - }); - $("#make").click(function(){ - - editor.insert("\nmake "); - }); - $("#call").click(function(){ - - editor.insert("\ncall "); - }); - $("#loop").click(function(){ - - editor.insert("\nmake loop"); - }); - $("#var").click(function(){ - - editor.insert("\nmake var "); - }); - -}); \ No newline at end of file + for ( var l = 1; l < script.length; l ++ ) { + + if ( script[l] !== '' ) { + + // parseInput( script[l] ); + global_object.code += parseInput( script[l] ) + '\n'; + } + } + + if ( global_object.code !== undefined ) { + + run(); + } + return; + } + + return catchError(`wordScript *error: '${ arg[0] }' is not a excutable wordScript file.`) + }); + }) +} \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index 270ac3e..0000000 --- a/index.html +++ /dev/null @@ -1,50 +0,0 @@ - - - - - WordScript - - - - - - - - - -

WordScript

-
-
-
-
-
-
-
-
-
-
- - - - - - - - diff --git a/main.css b/main.css deleted file mode 100644 index 4fe41dd..0000000 --- a/main.css +++ /dev/null @@ -1,54 +0,0 @@ -#input{ - height: 1000px; -} -body{ - background-color: #fff; -} -#output{ - height: 1000px; - z-index: 10; -} -.inputTxt{ - margin-left: -20px; - width: 120% !important; - float: left; - height:100% !important; - -} -#keyWord{ - float: left; - font-size: 20px; -} - -#keyList{ - margin-top: 30px !important; -} - -#editor { - position: absolute; - top: 0; - right: 0; - bottom: 0; - left: 0; - font-size: 15px; -} - -#editor2 { - position: absolute; - top: 0; - right: 0; - bottom: 0; - left: 0; - font-size: 15px; -} -#okay { - margin: auto; - width: 50%; - color:blue; - text-align: center; - -} -#okay:hover{ - color:blue; - text-decoration: none; -} \ No newline at end of file From 66912d761db213d5abd175b6f94122169783a45e Mon Sep 17 00:00:00 2001 From: Dre_ Date: Wed, 31 May 2017 21:13:06 +0000 Subject: [PATCH 2/6] commiting new chnages made to app.js and putting back up all the files I deleted files by addict --- app.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index db6368c..1f6be4b 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,3 @@ - var editor = ace.edit("editor"); // the numbering editor.setTheme("ace/theme/monokai"); // theme editor.getSession().setMode("ace/mode/javascript"); // language want to use @@ -121,7 +120,8 @@ function makeFunction( input ) { } else if ( body[0] === 'print' ) { - body = print( body.splice(1, body.length) ); + + body = print( body ); } else { @@ -136,6 +136,23 @@ function makeFunction( input ) { } + +function makeCondition( condition, input ) { + + input = input.split(' ').slice(1, input.length); + + if ( condition === 'if' ) { + + console.log( input ); + } + if ( condition === 'else' ) { + + console.log( input ); + } + + // console.log( input ); +} + function callFunction( input ) { for(var i = 0; i < data.func.length; i++) { @@ -283,6 +300,10 @@ function parseInput( input ) { break; } break; + case 'if': + + output = makeCondition('if', input); + break; case 'print': output = print( input ); From f563b1f442a6428b8de92094bc38b7356a1d1c64 Mon Sep 17 00:00:00 2001 From: Dre_ Date: Wed, 31 May 2017 21:17:15 +0000 Subject: [PATCH 3/6] commting last chnages to this repo --- app.js | 384 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 50 +++++++ main.css | 54 ++++++++ 3 files changed, 488 insertions(+) create mode 100644 app.js create mode 100644 index.html create mode 100644 main.css diff --git a/app.js b/app.js new file mode 100644 index 0000000..1f6be4b --- /dev/null +++ b/app.js @@ -0,0 +1,384 @@ +var editor = ace.edit("editor"); // the numbering +editor.setTheme("ace/theme/monokai"); // theme +editor.getSession().setMode("ace/mode/javascript"); // language want to use +editor.setValue("# Start your work here\n"); // adding a value +editor.session.setOption("useWorker", false); //disable the corrections + + +var editor2 = ace.edit("editor2"); // the numbering +editor2.setTheme("ace/theme/chrome"); // theme +editor2.getSession().setMode("ace/mode/javascript"); // language want to use +editor2.setReadOnly(true); // make the editor only read +editor2.setValue("// Javascript will appear here\n"); +editor2.session.setOption("useWorker", false); + + + + +var data = { + variables: [], + func: [], + wordGist: [], + code: '' +}; + +function includes( array, item ) { + + for ( var i in array ) { + if ( array[i] === item ) { + + return true; + } + } + + return false +} + +function run() { + + try { + eval( data.code ); + } + catch(e) { + console.log('*error: ' + e.message); + } +} + +function print( input ) { + + input = input.splice(1, input.length); + + if ( input.length === 1 ) { + + // return back the translated javascript code + return 'console.log('+ input.join(' ') +');'; + } + + /* + checks with the input is a variable already + declared in the data obj variables property + */ + for ( var i in data.variables ) { + + /* + if it exsit then return back the + stringifyed sjavascript code with + the value of the concated inside of the string + */ + if ( data.variables[i].name === input ) { + + return 'console.log('+ data.variables[i].value +');'; + } + } + + if ( input[0] === 'call' ) { + + var func = callFunction( input ); + // console.log( func.slice(0, func.length - 1) ); + return 'console.log( ' + func.slice(0, func.length - 1) + ' );'; + } + + +} + +// arg = [x,y,z]; +// "[x,y,z]" +// arg.slice(1, arg.length - 1) +// "x,y,z" +// arg.slice(1, arg.length - 1).split(',') +// ["x", "y", "z"] +function makeFunction( input ) { + + data.func = []; + + + var func_name = input[2]; + + data.func.push({ + func_name: input[2], + params: input[3][0] !== '[' ? null : input[3].slice(1, input[3].length - 1).split(','), + body: input.length === 7 ? input.splice(3, input.length).join(' ') : input.splice(4, input.length).join(' ') + }); + + // arg = [x,y,z]; + // "[x,y,z]" + // arg.slice(1, arg.length - 1) + // "x,y,z" + // arg.slice(1, arg.length - 1).split(',') + // ["x", "y", "z"] + + for(var i = 0; i < data.func.length; i++) { + + if (data.func[i].func_name === func_name ){ + + var body = data.func[i].body.split(' '); + + + if ( body[0] !== 'return' && body[0] !== 'print' ) { + + body = 'return ' + body.join(' ') + ';'; + } + else if ( body[0] === 'print' ) { + + + body = print( body ); + } + else { + + body = body.join(' ') +';'; + } + + + return "function " + data.func[i].func_name +"("+ data.func[i].params +") { \n \t" + body +" \n}"; + } + } + + +} + + +function makeCondition( condition, input ) { + + input = input.split(' ').slice(1, input.length); + + if ( condition === 'if' ) { + + console.log( input ); + } + if ( condition === 'else' ) { + + console.log( input ); + } + + // console.log( input ); +} + +function callFunction( input ) { + + for(var i = 0; i < data.func.length; i++) { + + if (data.func[i].func_name === input[1]){ + + for ( var i in data.variables ) { + + if ( input[input.length - 1] === data.variables[i] ) { + + return input[1] + '('+ data.variables[i] +');'; + } + } + + return input[1] + '('+ input[input.length - 1] +');'; + } + } + +} + + +function makeVariable( input ) { + + /* + slice the list to remove the word 'make' and 'var' + and only have '' ':' '' + */ + input = input.slice(2, input.length ); + + + /* + if the array's last offset is splited + to return back array which has a length + greater then 1 then follow this statement. + + ex: ':' + + and that it that the last frist offset in + the last item of the array is not a quotation. + */ + + if ( input[input.length - 1].length > 1 + && input[input.length - 1][0] !== "'" + && input[input.length - 1][0] !== 't' + && input[input.length - 1][0] !== 'f' ) { + + // split the last offset by the colon + input = input[input.length - 1].split(':'); + + /* + get the desired target values + from the return splited array + and push them to the data obj + to variable property. + */ + data.variables.push({ + name: input[0], + value: input[1], + }); + + + // lastly return the javascript code of the fromated variable + return 'var ' + input[0] + ' = ' + input[1] + ';' ; + } + + /* + if the codintion was not true then: + join the list then split by colon + + input before: [ '' ':' '' ] + input after: [ '' '' ] + */ + input = input.join(' ').split(':'); + + /* + get the desired variables in the + array new input array then puh them + to the data obj and tore them in + the variables property + */ + data.variables.push({ + name: input[0].split(' ').join(''), + value: input[1].split(' ').join('') + }); + + // lastly return the javascript code of the fromated variable + return 'var ' + input[0].split(' ').join('') + ' = ' + input[1].split(' ').join('') + ';'; + + +} + +// saves the code that the user word in wordScript +function makeWordGist() { + + /* + pushes the code to the + data obj to property wordGist + */ + data.wordGist.push({ + name: prompt('name'), + code: editor.getValue(), + translated_code: data.code + }); + +} + +// makes a loop +function makeLoop( input ) { + + // gets the input of the frist and last offset + var start = input[2]; + var end = input[input.length - 1]; + + // return the translated javascript code + return 'for ( var i = '+ start +'; i <= '+ end +'; i++ ) {\n console.log( i );\n}' +} + +// parsers the worScrit code +function parseInput( input ) { + + input = input.split(' '); + var output; + + // input data: 'make variable x : 3' + // output data: ['make' 'variable' 'x' ':' '3'] + + switch ( input[0].toLowerCase() ) { + + case 'make': + + switch ( input[1].toLowerCase() ) { + + case 'loop': + + output = makeLoop( input ); + break; + case 'var': + + // target index two as the variable + output = makeVariable( input ); + break; + case 'function': + + output = makeFunction( input ); + break; + } + break; + case 'if': + + output = makeCondition('if', input); + break; + case 'print': + + output = print( input ); + break; + case 'call': + + output = callFunction( input ); + break; + case '#': + + output = '// ' + input.splice(1, input.length).join(' '); + break; + default: + + output = input; + break; + } + + return output; +} + + + + +$(document).ready(function(){ + $("#editor").keydown(function(key){ + if(key.which === 13){ + + var input = editor.getValue().split('\n'); + + data.code = ''; + for(var i in input){ + data.code += parseInput( input[i] ) + '\n'; + + if ( data.code !== undefined ) { + editor2.setValue( data.code ); + } + } + } + }); + + $('#run').click(function() { + run(); + }); + + + $('#save').click(function() { + makeWordGist(); + }); + $("#print").click(function(){ + + editor.insert("\nprint "); + }); + $("#function").click(function(){ + editor.insert("\nmake function "); + }); + $("#parameter").click(function(){ + + editor.insert("\n[ ]"); + }); + $("#make").click(function(){ + + editor.insert("\nmake "); + }); + $("#call").click(function(){ + + editor.insert("\ncall "); + }); + $("#loop").click(function(){ + + editor.insert("\nmake loop"); + }); + $("#var").click(function(){ + + editor.insert("\nmake var "); + }); + +}); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..270ac3e --- /dev/null +++ b/index.html @@ -0,0 +1,50 @@ + + + + + WordScript + + + + + + +
+ + +

WordScript

+
+
+
+
+
+
+
+
+
+
+ + + + + + + + diff --git a/main.css b/main.css new file mode 100644 index 0000000..4fe41dd --- /dev/null +++ b/main.css @@ -0,0 +1,54 @@ +#input{ + height: 1000px; +} +body{ + background-color: #fff; +} +#output{ + height: 1000px; + z-index: 10; +} +.inputTxt{ + margin-left: -20px; + width: 120% !important; + float: left; + height:100% !important; + +} +#keyWord{ + float: left; + font-size: 20px; +} + +#keyList{ + margin-top: 30px !important; +} + +#editor { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + font-size: 15px; +} + +#editor2 { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + font-size: 15px; +} +#okay { + margin: auto; + width: 50%; + color:blue; + text-align: center; + +} +#okay:hover{ + color:blue; + text-decoration: none; +} \ No newline at end of file From bf1e2b4a4452b56e789dbd604b3fb95e5866493e Mon Sep 17 00:00:00 2001 From: Dre_ Date: Tue, 13 Jun 2017 22:17:31 +0000 Subject: [PATCH 4/6] added the cli tool for worScrit to compile the files with extension .ws in termial and run code display --- compiler.js | 382 ---------------------------------------------------- 1 file changed, 382 deletions(-) delete mode 100755 compiler.js diff --git a/compiler.js b/compiler.js deleted file mode 100755 index fd18c37..0000000 --- a/compiler.js +++ /dev/null @@ -1,382 +0,0 @@ -#!/usr/bin/env node - -var fs = require('fs'); - -var global_object = { - variables: [], - func: [], - wordGist: [], - code: '' -}; - -function includes( array, item ) { - - for ( var i in array ) { - if ( array[i] === item ) { - - return true; - } - } - - return false -} - -function run() { - - try { - - eval( global_object.code ); - } - catch(e) { - - catchError('wordScript *error: ' + e.message); - } -} - -function print( input ) { - - input = input.splice(1, input.length); - - if ( input.length === 1 ) { - - // return back the translated javascript code - return `console.log( ${ input.join(' ') } );`; - } - - /* - checks with the input is a variable already - declared in the data obj variables property - */ - for ( var i in global_object.variables ) { - - /* - if it exsit then return back the - stringifyed sjavascript code with - the value of the concated inside of the string - */ - if ( includes( input, global_object.variables[i].name ) ) { - - return `console.log( ${ input.join(' ') } );`; - } - } - - if ( input[0] === 'call' ) { - - var func = callFunction( input ); - - return `console.log( ${ func.slice(0, func.length - 1) } );`; - } - - return `console.log( ${ input.join(' ') } );` - -} - -// arg = [x,y,z]; -// "[x,y,z]" -// arg.slice(1, arg.length - 1) -// "x,y,z" -// arg.slice(1, arg.length - 1).split(',') -// ["x", "y", "z"] -function makeFunction( input ) { - - global_object.func = []; - - - var func_name = input[2]; - - global_object.func.push({ - func_name: input[2], - params: input[3][0] !== '[' ? null : input[3].slice(1, input[3].length - 1).split(','), - body: input.length === 7 ? input.splice(3, input.length).join(' ') : input.splice(4, input.length).join(' ') - }); - - // arg = [x,y,z]; - // "[x,y,z]" - // arg.slice(1, arg.length - 1) - // "x,y,z" - // arg.slice(1, arg.length - 1).split(',') - // ["x", "y", "z"] - - for(var i = 0; i < global_object.func.length; i++) { - - if (global_object.func[i].func_name === func_name ){ - - var body = global_object.func[i].body.split(' '); - - - if ( body[0] !== 'return' && body[0] !== 'print' ) { - - body = `return ${ body.join(' ') } ;`; - } - else if ( body[0] === 'print' ) { - - - body = print( body ); - } - else { - - body = `${ body.join(' ') } ;`; - } - - - return `function ${ global_object.func[i].func_name} ( ${ global_object.func[i].params } ) { \n \t ${ body } \n}`; - } - } - - -} - - -function makeCondition( condition, input ) { - - input = input.split(' ').slice(1, input.length); - - if ( condition === 'if' ) { - - console.log( input ); - } - if ( condition === 'else' ) { - - console.log( input ); - } - - // console.log( input ); -} - -function callFunction( input ) { - - for(var i = 0; i < global_object.func.length; i++) { - - if (global_object.func[i].func_name === input[1]){ - - for ( var i in global_object.variables ) { - - if ( input[input.length - 1] === global_object.variables[i] ) { - - return `${ input[1] } ( ${ global_object.variables[i] } );`; - } - } - - return `${ input[1] } ( ${ input[input.length - 1] } );`; - } - } - -} - - -function makeVariable( input ) { - - /* - slice the list to remove the word 'make' and 'var' - and only have '' ':' '' - */ - input = input.slice(2, input.length ); - - - /* - if the array's last offset is splited - to return back array which has a length - greater then 1 then follow this statement. - - ex: ':' - - and that it that the last frist offset in - the last item of the array is not a quotation. - */ - - if ( input[input.length - 1].length > 1 - && input[input.length - 1][0] !== "'" - && input[input.length - 1][0] !== 't' - && input[input.length - 1][0] !== 'f' ) { - - // split the last offset by the colon - input = input[input.length - 1].split(':'); - - /* - get the desired target values - from the return splited array - and push them to the data obj - to variable property. - */ - global_object.variables.push({ - name: input[0], - value: input[1], - }); - - - // lastly return the javascript code of the fromated variable - return `var ${ input[0] } = ${ input[1] };`; - } - - /* - if the codintion was not true then: - join the list then split by colon - - input before: [ '' ':' '' ] - input after: [ '' '' ] - */ - input = input.join(' ').split(':'); - - /* - get the desired variables in the - array new input array then puh them - to the data obj and tore them in - the variables property - */ - global_object.variables.push({ - name: input[0].split(' ').join(''), - value: input[1].split(' ').join('') - }); - - // lastly return the javascript code of the fromated variable - return `var ${ input[0].split(' ').join('') } = ${ input[1].split(' ').join('') };`; - - -} - -// saves the code that the user word in wordScript -/*function makeWordGist() { - - - pushes the code to the - data obj to property wordGist - - data.wordGist.push({ - name: prompt('name'), - code: editor.getValue(), - translated_code: data.code - }); - -}*/ - -// makes a loop -function makeLoop( input ) { - - // gets the input of the frist and last offset - - - if ( includes(input, ':') ) { - - if ( includes(input, 'print') ) { - - return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ print( input.splice( input.indexOf('print'), input.length ) ) } \n}`; - } - else { - - return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ input.splice(input.indexOf(':') + 1, input.length).join(' ') } \n}`; - } - } - - // return the translated javascript code - return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n console.log( i ); \n}`; -} - - - -function catchError( err, callback ) { - - - if ( callback === undefined ) { - - console.log( err || `*error: ${ err.message ? err.message : 'error returned ' + err }` ); - return; - } - else if ( callback !== undefined ) { - - return callback({ - status: false, - msg: err || `*error: ${ err.message.split(' ').splice(1, err.message.split(' ').length).join(' ') }` - }, undefined); - } -} - - -// parsers the worScrit code -function parseInput( input ) { - - input = input.split(' '); - var output; - - // input data: 'make variable x : 3' - // output data: ['make' 'variable' 'x' ':' '3'] - - switch ( input[0].toLowerCase() ) { - - case 'make': - - switch ( input[1].toLowerCase() ) { - - case 'loop': - - output = makeLoop( input ); - break; - case 'var': - - // target index two as the variable - output = makeVariable( input ); - break; - case 'function': - - output = makeFunction( input ); - break; - } - break; - case 'if': - - output = makeCondition('if', input); - break; - case 'print': - - output = print( input ); - break; - case 'call': - - output = callFunction( input ); - break; - case '#': - - output = '// ' + input.splice(1, input.length).join(' '); - break; - default: - - output = input; - break; - } - - return output; -} - -var arg = process.argv.splice(process.argv.length - 1, process.argv.length); - -if ( arg.length !== 0 ) { - - fs.exists(`${ __dirname }/${ arg }`, ( err, rsp ) => { - if ( !err ) return catchError( err ); - - fs.readFile(arg[0], 'utf8', ( err, data ) => { - if ( err ) return catchError( err ); - - var script = data.split('\n'); - - if ( script[0] === '' ) { - - for ( var l = 1; l < script.length; l ++ ) { - - if ( script[l] !== '' ) { - - // parseInput( script[l] ); - global_object.code += parseInput( script[l] ) + '\n'; - } - } - - if ( global_object.code !== undefined ) { - - run(); - } - return; - } - - return catchError(`wordScript *error: '${ arg[0] }' is not a excutable wordScript file.`) - }); - }) -} \ No newline at end of file From fd03c9b9fc319333fbd818d7700ba831c708abc6 Mon Sep 17 00:00:00 2001 From: Dre_ Date: Tue, 13 Jun 2017 22:17:52 +0000 Subject: [PATCH 5/6] added the cli tool for worScrit to compile the files with extension .ws in termial and run code display --- wordScript-cli/app.ws | 11 ++ wordScript-cli/cli.js | 25 +++ wordScript-cli/clib | 1 + wordScript-cli/compiler.js | 374 +++++++++++++++++++++++++++++++++++++ 4 files changed, 411 insertions(+) create mode 100644 wordScript-cli/app.ws create mode 100644 wordScript-cli/cli.js create mode 160000 wordScript-cli/clib create mode 100644 wordScript-cli/compiler.js diff --git a/wordScript-cli/app.ws b/wordScript-cli/app.ws new file mode 100644 index 0000000..b4f3461 --- /dev/null +++ b/wordScript-cli/app.ws @@ -0,0 +1,11 @@ +make var x : "hello, world" + +# print x + +# make loop 1 to 10 + +# make function addTwo [y] return y + 2 + +# print call addTwo with args x.length + +print x.length + 2 \ No newline at end of file diff --git a/wordScript-cli/cli.js b/wordScript-cli/cli.js new file mode 100644 index 0000000..8297a83 --- /dev/null +++ b/wordScript-cli/cli.js @@ -0,0 +1,25 @@ +const clib = require('./clib'), +wordScript = require('./compiler.js'); + +function method(action, obj) { + + switch ( action ) { + + case 'run': + + console.log('compiling....'); + + const file_name = obj.payload[0]; + + wordScript.compile( file_name ); + break; + } +} + + +clib.argParser(process.argv, (err, obj) => { + if (err) return clib.catchError(err); + + + clib.dispatch_action(method, obj); +}); \ No newline at end of file diff --git a/wordScript-cli/clib b/wordScript-cli/clib new file mode 160000 index 0000000..ca23f23 --- /dev/null +++ b/wordScript-cli/clib @@ -0,0 +1 @@ +Subproject commit ca23f237db140bbcc5fb37fd13b9418b9a6d7608 diff --git a/wordScript-cli/compiler.js b/wordScript-cli/compiler.js new file mode 100644 index 0000000..720fa0d --- /dev/null +++ b/wordScript-cli/compiler.js @@ -0,0 +1,374 @@ +var fs = require('fs'); + +var global_object = { + variables: [], + func: [], + wordGist: [], + code: '' +}; + +function includes( array, item ) { + + for ( var i in array ) { + if ( array[i] === item ) { + + return true; + } + } + + return false +} + +function run() { + + try { + + eval( global_object.code ); + } + catch(e) { + + catchError('wordScript *error: ' + e.message); + } +} + +function print( input ) { + + input = input.splice(1, input.length); + + if ( input.length === 1 ) { + + // return back the translated javascript code + return `console.log( ${ input.join(' ') } );`; + } + + /* + checks with the input is a variable already + declared in the data obj variables property + */ + for ( var i in global_object.variables ) { + + /* + if it exsit then return back the + stringifyed sjavascript code with + the value of the concated inside of the string + */ + if ( includes( input, global_object.variables[i].name ) ) { + + return `console.log( ${ input.join(' ') } );`; + } + } + + if ( input[0] === 'call' ) { + + var func = callFunction( input ); + + return `console.log( ${ func.slice(0, func.length - 1) } );`; + } + + return `console.log( ${ input.join(' ') } );` + +} + +// arg = [x,y,z]; +// "[x,y,z]" +// arg.slice(1, arg.length - 1) +// "x,y,z" +// arg.slice(1, arg.length - 1).split(',') +// ["x", "y", "z"] +function makeFunction( input ) { + + global_object.func = []; + + + var func_name = input[2]; + + global_object.func.push({ + func_name: input[2], + params: input[3][0] !== '[' ? null : input[3].slice(1, input[3].length - 1).split(','), + body: input.length === 7 ? input.splice(3, input.length).join(' ') : input.splice(4, input.length).join(' ') + }); + + // arg = [x,y,z]; + // "[x,y,z]" + // arg.slice(1, arg.length - 1) + // "x,y,z" + // arg.slice(1, arg.length - 1).split(',') + // ["x", "y", "z"] + + for(var i = 0; i < global_object.func.length; i++) { + + if (global_object.func[i].func_name === func_name ){ + + var body = global_object.func[i].body.split(' '); + + + if ( body[0] !== 'return' && body[0] !== 'print' ) { + + body = `return ${ body.join(' ') } ;`; + } + else if ( body[0] === 'print' ) { + + + body = print( body ); + } + else { + + body = `${ body.join(' ') } ;`; + } + + + return `function ${ global_object.func[i].func_name} ( ${ global_object.func[i].params } ) { \n \t ${ body } \n}`; + } + } + + +} + + +function makeCondition( condition, input ) { + + input = input.split(' ').slice(1, input.length); + + if ( condition === 'if' ) { + + console.log( input ); + } + if ( condition === 'else' ) { + + console.log( input ); + } + + // console.log( input ); +} + +function callFunction( input ) { + + for(var i = 0; i < global_object.func.length; i++) { + + if (global_object.func[i].func_name === input[1]){ + + for ( var i in global_object.variables ) { + + if ( input[input.length - 1] === global_object.variables[i] ) { + + return `${ input[1] } ( ${ global_object.variables[i] } );`; + } + } + + return `${ input[1] } ( ${ input[input.length - 1] } );`; + } + } + +} + + +function makeVariable( input ) { + + /* + slice the list to remove the word 'make' and 'var' + and only have '' ':' '' + */ + input = input.slice(2, input.length ); + + + /* + if the array's last offset is splited + to return back array which has a length + greater then 1 then follow this statement. + + ex: ':' + + and that it that the last frist offset in + the last item of the array is not a quotation. + */ + + if ( input[input.length - 1].length > 1 + && input[input.length - 1][0] !== "'" + && input[input.length - 1][0] !== 't' + && input[input.length - 1][0] !== 'f' ) { + + // split the last offset by the colon + input = input.join(' ').split(':'); + + /* + get the desired target values + from the return splited array + and push them to the data obj + to variable property. + */ + global_object.variables.push({ + name: input[0], + value: input[1], + }); + + + // lastly return the javascript code of the fromated variable + return `var ${ input[0] } = ${ input[1] };`; + } + + /* + if the codintion was not true then: + join the list then split by colon + + input before: [ '' ':' '' ] + input after: [ '' '' ] + */ + input = input.join(' ').split(':'); + + /* + get the desired variables in the + array new input array then puh them + to the data obj and tore them in + the variables property + */ + global_object.variables.push({ + name: input[0].split(' ').join(''), + value: input[1].split(' ').join('') + }); + + // lastly return the javascript code of the fromated variable + return `var ${ input[0].split(' ').join('') } = ${ input[1].split(' ').join('') };`; + + +} + +// saves the code that the user word in wordScript +/*function makeWordGist() { + + + pushes the code to the + data obj to property wordGist + + data.wordGist.push({ + name: prompt('name'), + code: editor.getValue(), + translated_code: data.code + }); + +}*/ + +// makes a loop +function makeLoop( input ) { + + // gets the input of the frist and last offset + + + if ( includes(input, ':') ) { + + if ( includes(input, 'print') ) { + + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ print( input.splice( input.indexOf('print'), input.length ) ) } \n}`; + } + else { + + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n ${ input.splice(input.indexOf(':') + 1, input.length).join(' ') } \n}`; + } + } + + // return the translated javascript code + return `for ( var i = ${ input[ input.indexOf('to') - 1 ] }; i <= ${ input[ input.indexOf('to') + 1 ] }; i++ ) {\n console.log( i ); \n}`; +} + + + +function catchError( err, callback ) { + + + if ( callback === undefined ) { + + console.log( err || `*error: ${ err.message ? err.message : 'error returned ' + err }` ); + return; + } + else if ( callback !== undefined ) { + + return callback({ + status: false, + msg: err || `*error: ${ err.message.split(' ').splice(1, err.message.split(' ').length).join(' ') }` + }, undefined); + } +} + + +// parsers the worScrit code +function parseInput( input ) { + + input = input.split(' '); + var output; + + // input data: 'make variable x : 3' + // output data: ['make' 'variable' 'x' ':' '3'] + + switch ( input[0].toLowerCase() ) { + + case 'make': + + switch ( input[1].toLowerCase() ) { + + case 'loop': + + output = makeLoop( input ); + break; + case 'var': + + // target index two as the variable + output = makeVariable( input ); + break; + case 'function': + + output = makeFunction( input ); + break; + } + break; + case 'if': + + output = makeCondition('if', input); + break; + case 'print': + + output = print( input ); + break; + case 'call': + + output = callFunction( input ); + break; + case '#': + + output = '// ' + input.splice(1, input.length).join(' '); + break; + default: + + output = input; + break; + } + + return output; +} + +module.exports.compile = function( file_name ) { + + fs.exists(`${ __dirname }/${ file_name }`, ( err, rsp ) => { + if ( !err ) return catchError( err ); + + fs.readFile(file_name, 'utf8', ( err, data ) => { + if ( err ) return catchError( err ); + + var script = data.split('\n'); + + for ( var l = 0; l < script.length; l ++ ) { + + if ( script[l] !== '' ) { + + // parseInput( script[l] ); + global_object.code += parseInput( script[l] ) + '\n'; + } + } + + if ( global_object.code !== undefined ) { + + run(); + return; + } + }) + + }) +} \ No newline at end of file From 2ffc6c7f1138411370b3dd135f15216cad571120 Mon Sep 17 00:00:00 2001 From: andreGarvin Date: Sat, 14 Oct 2017 16:12:15 -0400 Subject: [PATCH 6/6] Update README.md --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7b4f77a..5a88043 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,24 @@ # wordScript: easy programming langauge ( without semicolon's ) This Project is to help people that don't know how to code or have trouble understanding code to make coding easiler by using keywords to start code. +
We built this project by using HTML,CSS and javascript. The challenges we ran into is how to get the display to look like real programming sites with the numbers and in the javascript we had some problems with names and code, it was everywhere so it was hard to reorganized it -We learned a lot with a text editor libary called ACE and we learned how to create keywords in the program and transform the keywords into javascript +We learned a lot with a text editor libary called ACE and we learned how to create keywords in the program and transform the keywords into javascript. +
What's next with this project is to incorporate more software languages with text editors and add in a better system so people are able to understand how code work more easily - -# features: +# language: ``` //Start your work here # this a comment in wordScript + make var x:3 + make function addTwo [x] return x + 2] -call addTwo with args 3 + +# call addTwo with args 3 + print 'hi' + +make loop 1 to 5 ```