Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
1fc41c44
Commit
1fc41c44
authored
Feb 27, 2015
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved code to find existing variables with their types associate into the static_typing.js file.
parent
3b0457df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
29 deletions
+51
-29
core/static_typing.js
core/static_typing.js
+45
-1
generators/arduino.js
generators/arduino.js
+6
-28
No files found.
core/static_typing.js
View file @
1fc41c44
...
...
@@ -14,6 +14,8 @@ goog.provide('Blockly.StaticTyping');
goog
.
require
(
'
Blockly.Block
'
);
goog
.
require
(
'
Blockly.Workspace
'
);
/**
* Navigates through the child blocks to get the block type.
...
...
@@ -27,7 +29,7 @@ Blockly.StaticTyping.getChildBlockType = function(block) {
(
nextBlock
[
0
].
getChildren
().
length
>
0
))
{
nextBlock
=
nextBlock
[
0
].
getChildren
();
}
if
(
nextBlock
[
0
]
===
this
)
{
if
(
nextBlock
[
0
]
===
block
)
{
// Set variable block is empty, so no type yet
blockType
=
'
defineme
'
;
//varType = 'int';
...
...
@@ -59,3 +61,45 @@ Blockly.StaticTyping.findListVarType = function(varToFind, existingVars) {
}
return
null
;
};
/**
*
* @param {string} varToFind String containing the name of the variable to find.
* @param {Blockly.Workspace} workspace workspace to collect variables from.
* @return {Array<string>} Associative array with the variable names as the keys
* and the type as the values.
*/
Blockly
.
StaticTyping
.
getAllVarsWithTypes
=
function
(
workspace
)
{
var
blocks
;
if
(
workspace
.
getAllBlocks
)
{
blocks
=
workspace
.
getAllBlocks
();
}
else
{
throw
'
Not valid workspace:
'
+
root
;
}
var
variableTypes
=
Object
.
create
(
null
);
for
(
var
x
=
0
;
x
<
blocks
.
length
;
x
++
)
{
var
getVars
=
blocks
[
x
].
getVars
;
if
(
getVars
)
{
// Iterate through the variables used in this block
var
blockVariables
=
getVars
.
call
(
blocks
[
x
]);
for
(
var
y
=
0
;
y
<
blockVariables
.
length
;
y
++
)
{
// Send variable list to getVarType, returns type if first encounter or
// null if already defined.
var
getVarType
=
blocks
[
x
].
getVarType
;
if
(
getVarType
)
{
var
varType
=
getVarType
.
call
(
blocks
[
x
],
variableTypes
);
if
(
varType
!==
null
)
{
variableTypes
[
blockVariables
[
y
]]
=
varType
;
}
}
else
{
//TODO: Once all static typing code is done, default this to 'int'
//variableTypes[blockVariables[y]] = 'getVarTypeNotDef';
}
}
}
}
return
variableTypes
;
};
generators/arduino.js
View file @
1fc41c44
...
...
@@ -120,10 +120,9 @@ profile["default"] = profile["arduino"];
/**
* Initialise the database of variable names.
* @param {Blockly.Workspace=} opt_workspace Workspace to generate code from.
* Defaults to main workspace.
* @param {Blockly.Workspace} workspace Workspace to generate code from.
*/
Blockly
.
Arduino
.
init
=
function
(
opt_
workspace
)
{
Blockly
.
Arduino
.
init
=
function
(
workspace
)
{
// Create a dictionary of definitions to be printed before setups.
Blockly
.
Arduino
.
definitions_
=
Object
.
create
(
null
);
// Create a dictionary of setups to be printed before the code.
...
...
@@ -142,31 +141,10 @@ Blockly.Arduino.init = function(opt_workspace) {
}
// Iterate through the blocks to capture variables with first instance type
var
variableTypes
=
Object
.
create
(
null
);
var
blocks
=
Blockly
.
mainWorkspace
.
getAllBlocks
();
for
(
var
x
=
0
;
x
<
blocks
.
length
;
x
++
)
{
var
getVars
=
blocks
[
x
].
getVars
;
if
(
getVars
)
{
// Iterate through the variables used in this block
var
blockVariables
=
getVars
.
call
(
blocks
[
x
]);
for
(
var
y
=
0
;
y
<
blockVariables
.
length
;
y
++
)
{
// Send variable list to getVarType, returns type if first encounter or
// null if already defined.
var
getVarType
=
blocks
[
x
].
getVarType
;
if
(
getVarType
)
{
var
varType
=
getVarType
.
call
(
blocks
[
x
],
variableTypes
);
if
(
varType
!=
null
)
{
variableTypes
[
blockVariables
[
y
]]
=
varType
;
}
}
else
{
//TODO: Once all static typing code is done, default this to 'int'
//variableTypes[blockVariables[y]] = 'getVarTypeNotDef';
}
}
}
}
var
variableTypes
=
Blockly
.
StaticTyping
.
getAllVarsWithTypes
(
workspace
);
// The procedure arguments need to have all the variables collected first
var
blocks
=
workspace
.
getAllBlocks
();
for
(
var
x
=
0
;
x
<
blocks
.
length
;
x
++
)
{
var
setArgsType
=
blocks
[
x
].
setArgsType
;
if
(
setArgsType
)
{
...
...
@@ -176,8 +154,8 @@ Blockly.Arduino.init = function(opt_workspace) {
// Set variable declarations
var
variableDeclarations
=
[];
for
(
var
n
ame
in
variableTypes
)
{
variableDeclarations
.
push
(
variableTypes
[
name
]
+
'
'
+
n
ame
+
'
;
'
);
for
(
var
varN
ame
in
variableTypes
)
{
variableDeclarations
.
push
(
variableTypes
[
varName
]
+
'
'
+
varN
ame
+
'
;
'
);
}
Blockly
.
Arduino
.
definitions_
[
'
variables
'
]
=
variableDeclarations
.
join
(
'
\n
'
)
+
'
\n
'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment