Skip to content

Commit 446f419

Browse files
committed
Added method to call functions and pass parameters
1 parent dd56b7e commit 446f419

File tree

4 files changed

+131
-62
lines changed

4 files changed

+131
-62
lines changed

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ <h2>Contribute</h2>
4444
</div>
4545

4646
<script src="js/voicerecog.js"></script>
47+
<script src="js/functionCalls.js"></script>
4748
<script src="js/buttonFunctions.js"></script>
4849
<script src="js/utilFunctions.js"></script>
4950
<script src="js/includeLibraries.js"></script>

js/functionCalls.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Code to implement a function call when a user says 'call'
2+
//Multiple parameters can be passed by saying 'pass' and then the name of the variable to be passed as parameter
3+
4+
5+
function callFunctions(){
6+
var functionStatement = splitWords[1]
7+
8+
9+
for (var i = 2; i < splitWords.length && splitWords[i] != 'pass'; i++) {
10+
functionStatement += splitWords[i].replace(/^./, splitWords[i][0].toUpperCase()); // Function name should be camel case
11+
}
12+
13+
functionStatement+=' (';
14+
for(var j=i;j<splitWords.length;++j){
15+
if(splitWords[j]=='pass' && j!=splitWords.length -1){
16+
functionStatement += takeParameter(j);
17+
}
18+
}
19+
20+
21+
functionStatement += ')';
22+
console.log(functionStatement);
23+
24+
25+
// add function to program textarea
26+
programTextArea.executeEdits("", [{
27+
range: {
28+
startLineNumber: programTextArea.getPosition().lineNumber,
29+
startColumn: programTextArea.getPosition().column,
30+
endLineNumber: programTextArea.getPosition().lineNumber,
31+
endColumn: programTextArea.getPosition().column
32+
},
33+
text: functionStatement + '\n',
34+
forceMoveMarkers: true
35+
}]);
36+
37+
38+
39+
}
40+
41+
//Function that takes parameters whenever the word 'pass' is said by the user
42+
function takeParameter(pos){
43+
var paramName = splitWords[pos+1];
44+
for(var k = pos+2; k<splitWords.length && splitWords[k]!='pass'; ++k){
45+
paramName += splitWords[k].replace(/^./, splitWords[k][0].toUpperCase()); //parameters name should be camelCase
46+
47+
}
48+
console.log(k);
49+
if(k!==splitWords.length) //if it is the last parameter passed then don't append a comma otherwise append one
50+
paramName += ' ,';
51+
return paramName;
52+
53+
54+
55+
}

js/functions.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function newFunction() {
55

66
// function name continues till user says 'takes' and starts giving parameters
77
var funcName = splitWords[2];
8+
89
for (i = 3; i < splitWords.length && splitWords[i] != 'takes'; i++) {
910
funcName += splitWords[i].replace(/^./, splitWords[i][0].toUpperCase()); // Function name should be camel case
1011
}

js/voicerecog.js

+74-62
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,94 @@
11
var recognition = new webkitSpeechRecognition();
22
recognition.continuous = true;
33
recognition.interimResults = true;
4-
recognition.lang = 'en-IN'; //recognition locale
4+
recognition.lang = "en-IN"; //recognition locale
55
var recognizing = false; //is the app listening for voice?
66
//data types array
7-
var dataTypes = ['int', 'char', 'float', 'double', 'void'];
7+
var dataTypes = ["int", "char", "float", "double", "void"];
88
//format specifiers for printf and scanf variables
9-
var identifiers = {int:'d', float:'f', double:'lf', char:'c'};
9+
var identifiers = { int: "d", float: "f", double: "lf", char: "c" };
10+
var passArray = ["pass"];
1011
// translation dictionary for pronunciation correction for commonly misinterpreted words
11-
var translationDictionary = {integer:'int', mean:'main', character:'char', '=':'equals', 'percent':'%', 'backslash':'\\', percentage:'%', line:'\\n'};
12+
var translationDictionary = {
13+
integer: "int",
14+
mean: "main",
15+
character: "char",
16+
"=": "equals",
17+
percent: "%",
18+
backslash: "\\",
19+
percentage: "%",
20+
line: "\\n",
21+
cal : "call",
22+
};
1223
var programTextArea; //text area to write programs
1324
var includeStatements;
1425
var variables = {}; //object containing variables that the user declares
1526
var splitWords = []; //word array for recognition
1627
var indent = 0; //indentation level, increment when adding braces
1728
var doLoopCalled = 0; //To check if a do-while loop is called
1829

19-
20-
2130
//handles voice recognition complete event (for one line)
22-
recognition.onresult = function(event) {
23-
24-
// split results into word array
25-
for (var i = event.resultIndex; i < event.results.length; i++) {
26-
if (event.results[event.results.length - 1].isFinal) {
27-
splitWords = event.results[event.results.length - 1][0].transcript.trim().split(' ');
28-
// console.log(splitWords);
29-
}
30-
}
31-
32-
// translate apppropriate words using translation dictionary
33-
for (var i = 0; i < splitWords.length; i++) {
34-
if (translationDictionary[splitWords[i]] != undefined) {
35-
splitWords[i] = translationDictionary[splitWords[i]];
36-
}
31+
recognition.onresult = function (event) {
32+
// split results into word array
33+
for (var i = event.resultIndex; i < event.results.length; i++) {
34+
if (event.results[event.results.length - 1].isFinal) {
35+
splitWords = event.results[event.results.length - 1][0].transcript
36+
.trim()
37+
.split(" ");
38+
console.log(splitWords);
3739
}
40+
}
3841

39-
//call function according to command (first word(s) spoken)
40-
if (splitWords[0] === 'include') {
41-
includeLibrary();
42-
}
43-
if (splitWords[0] == 'function') {
44-
newFunction();
45-
}
46-
if (arrayContains(dataTypes, splitWords[0])) {
47-
newVariable();
48-
}
49-
if (splitWords[0] === 'print') {
50-
printf();
51-
}
52-
if (splitWords[0] === 'scan') {
53-
scanf();
54-
}
55-
if (splitWords[0] === 'if') {
56-
ifStatement();
57-
}
58-
if(splitWords[0] === 'while'){
59-
whileLoop();
60-
}
61-
if(splitWords[0]==='do'){
62-
doLoop();
63-
doLoopCalled=1;
64-
}
65-
if(splitWords[0]==='for'){
66-
forLoop();
67-
}
68-
if(splitWords[0]=='out'){
69-
braceOut();
70-
}
71-
if(splitWords[0]==='else' && splitWords[1] !== 'if'){
72-
elseStatement();
73-
}
74-
if(splitWords[0]==='else' && splitWords[1]==='if'){
75-
elseIfStatement();
42+
// translate apppropriate words using translation dictionary
43+
for (var i = 0; i < splitWords.length; i++) {
44+
if (translationDictionary[splitWords[i]] != undefined) {
45+
splitWords[i] = translationDictionary[splitWords[i]];
7646
}
47+
}
7748

78-
//reset transcript and word array for next line
79-
transcript = '';
80-
splitWords = [];
49+
//call function according to command (first word(s) spoken)
50+
if (splitWords[0] === "include") {
51+
includeLibrary();
52+
}
53+
if (splitWords[0] == "function") {
54+
newFunction();
55+
}
56+
if (arrayContains(dataTypes, splitWords[0])) {
57+
newVariable();
58+
}
59+
if (splitWords[0] === "print") {
60+
printf();
61+
}
62+
if (splitWords[0] === "scan") {
63+
scanf();
64+
}
65+
if (splitWords[0] === "if") {
66+
ifStatement();
67+
}
68+
if (splitWords[0] === "while") {
69+
whileLoop();
70+
}
71+
if (splitWords[0] === "do") {
72+
doLoop();
73+
doLoopCalled = 1;
74+
}
75+
if (splitWords[0] === "for") {
76+
forLoop();
77+
}
78+
if (splitWords[0] == "out") {
79+
braceOut();
80+
}
81+
if (splitWords[0] === "else" && splitWords[1] !== "if") {
82+
elseStatement();
83+
}
84+
if (splitWords[0] === "else" && splitWords[1] === "if") {
85+
elseIfStatement();
86+
}
87+
if(splitWords[0]==='call'){
88+
callFunctions();
89+
}
8190

82-
}
91+
//reset transcript and word array for next line
92+
transcript = "";
93+
splitWords = [];
94+
};

0 commit comments

Comments
 (0)