Skip to content
Closed
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
4 changes: 4 additions & 0 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@ public void setModified(boolean state) {
//new Exception().printStackTrace();
current.setModified(state);
calcModified();
// Remove any status notice, fx. done saving or done uploading,
// to avoid confusion about if the current code is the version
//in the board or not while debugging.
editor.statusNotice("");
}


Expand Down
55 changes: 36 additions & 19 deletions app/src/processing/app/preproc/PdePreprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
*/
public class PdePreprocessor {
// stores number of built user-defined function prototypes
public int prototypeCount = 0;
public int generatedPrototypeCount = 0;

// stores number of included library headers written
// we always write one header: Arduino.h
public int headerCount = 1;

// the prototypes that are generated by the preprocessor
List<String> prototypes;
List<String> generatedPrototypes;

// these ones have the .* at the end, since a class name might be at the end
// instead of .* which would make trouble other classes using this can lop
Expand Down Expand Up @@ -126,10 +126,10 @@ public int writePrefix(String program, String buildPath,
// }
// }

prototypes = prototypes(program);
generatedPrototypes = generatePrototypes(program);

// store # of prototypes so that line number reporting can be adjusted
prototypeCount = prototypes.size();
generatedPrototypeCount = generatedPrototypes.size();

// do this after the program gets re-combobulated
this.program = program;
Expand All @@ -138,7 +138,7 @@ public int writePrefix(String program, String buildPath,
File streamFile = new File(buildPath, name + ".cpp");
stream = new PrintStream(new FileOutputStream(streamFile));

return headerCount + prototypeCount;
return headerCount + generatedPrototypeCount;
}


Expand Down Expand Up @@ -187,26 +187,26 @@ static String substituteUnicode(String program) {
//public String write(String program, String buildPath, String name,
// String extraImports[]) throws java.lang.Exception {
public String write() throws java.lang.Exception {
writeProgram(stream, program, prototypes);
writeProgram(stream, program, generatedPrototypes);
writeFooter(stream);
stream.close();

return name;
}

// Write the pde program to the cpp file
protected void writeProgram(PrintStream out, String program, List<String> prototypes) {
int prototypeInsertionPoint = firstStatement(program);
protected void writeProgram(PrintStream out, String program, List<String> generatedPrototypes) {
int generatedPrototypeInsertionPoint = firstStatement(program);

out.print(program.substring(0, prototypeInsertionPoint));
out.print(program.substring(0, generatedPrototypeInsertionPoint));
out.print("#include \"Arduino.h\"\n");

// print user defined prototypes
for (int i = 0; i < prototypes.size(); i++) {
out.print(prototypes.get(i) + "\n");
for (int i = 0; i < generatedPrototypes.size(); i++) {
out.print(generatedPrototypes.get(i) + "\n");
}

out.print(program.substring(prototypeInsertionPoint));
out.print(program.substring(generatedPrototypeInsertionPoint));
}


Expand Down Expand Up @@ -311,18 +311,35 @@ private String collapseBraces(String in) {
return buffer.toString();
}

public ArrayList<String> prototypes(String in) {
public ArrayList<String> generatePrototypes(String in) {
in = collapseBraces(strip(in));

// XXX: doesn't handle ... varargs
// XXX: doesn't handle function pointers
Pattern pattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*\\{)");
Pattern prototypePattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*;)");
Pattern functionPattern = Pattern.compile("[\\w\\[\\]\\*]+\\s+[&\\[\\]\\*\\w\\s]+\\([&,\\[\\]\\*\\w\\s]*\\)(?=\\s*\\{)");

ArrayList<String> matches = new ArrayList<String>();
Matcher matcher = pattern.matcher(in);
while (matcher.find())
matches.add(matcher.group(0) + ";");
// Find already declared prototypes
ArrayList<String> prototypeMatches = new ArrayList<String>();
Matcher prototypeMatcher = prototypePattern.matcher(in);
while (prototypeMatcher.find())
prototypeMatches.add(prototypeMatcher.group(0) + ";");

// Find all functions and generate prototypes for them
ArrayList<String> functionMatches = new ArrayList<String>();
Matcher functionMatcher = functionPattern.matcher(in);
while (functionMatcher.find())
functionMatches.add(functionMatcher.group(0) + ";");

// Remove generated prototypes that exactly match ones found in the source file
for (int functionIndex=0; functionIndex < functionMatches.size(); functionIndex++) {
for (int prototypeIndex=0; prototypeIndex < prototypeMatches.size(); prototypeIndex++) {
if ((functionMatches.get(functionIndex)).equals(prototypeMatches.get(prototypeIndex))) {
functionMatches.remove(functionIndex);
}
}
}

return matches;
return functionMatches;
}
}