- Fill in variable declarations with a default value.
- Various fixes here and there (local variable declarations, scope...).

Lots of stuff hapenning in mbed.
This commit is contained in:
Jonathan Protzenko 2015-05-27 14:10:25 -07:00
Родитель 0891b95dad
Коммит 1f094656ca
2 изменённых файлов: 31 добавлений и 7 удалений

Просмотреть файл

@ -76,7 +76,8 @@ module TDev {
}
public visitExprHolder(env: EmitterEnv, locals: J.JLocalDef[], expr: J.JExprHolder) {
var decls = locals.map(d => this.visit(env, d));
var decls = locals.map(d =>
this.visit(env, d) + " = " + H.defaultValueForType(d.type) + ";");
return decls.join("\n"+env.indent) +
(decls.length ? "\n" + env.indent : "") +
this.visit(env, expr);
@ -94,15 +95,16 @@ module TDev {
}
public visitLocalDef(env: EmitterEnv, name: string, id: string, type: J.JTypeRef) {
return H.mkType(this.libraryMap, type)+" "+H.mangle(name, id)+";";
return H.mkType(this.libraryMap, type)+" "+H.mangle(name, id);
}
// Allows the target to redefine their own string type.
public visitStringLiteral(env: EmitterEnv, s: string) {
return '"'+s.replace(/["\\\n]/g, c => {
return 'touch_develop::mk_string("'+s.replace(/["\\\n]/g, c => {
if (c == '"') return '\\"';
if (c == '\\') return '\\\\';
if (c == "\n") return '\\n';
}) + '"';
}) + '")';
}
public visitNumberLiteral(env: EmitterEnv, n: number) {
@ -155,8 +157,17 @@ module TDev {
// Is this a call in the current scope?
var scoped = H.isScopedCall(receiver);
if (scoped)
// We're always in the "right" scope thanks to C++ namespaces.
return H.mangleName(name);
if (this.libRef)
// If compiling a library, no scope actually means the library's
// scope. This step is required to possibly find a shim. This means
// that we may generate "lib::f()" where we could've just written
// "f()", but since the prototypes have been written out already,
// that's fine.
return this.resolveCall(this.libRef, name);
else
// Call to a function from the current script.
return H.mangleName(name);
// Is this a call to a library?
var n = H.isLibrary(receiver);
@ -248,7 +259,8 @@ module TDev {
}
public visitGlobalDef(e: EmitterEnv, name: string, t: J.JTypeRef) {
return e.indent + H.mkType(this.libraryMap, t) + " " + H.mangleName(name) + ";";
return e.indent + H.mkType(this.libraryMap, t) + " " + H.mangleName(name) +
" = " + H.defaultValueForType(t) + ";"
}
public visitAction(

Просмотреть файл

@ -74,6 +74,16 @@ module TDev {
}
}
export function defaultValueForType(t1: J.JTypeRef) {
var t = <any> t1;
if (t == "Number")
return "0";
else if (t == "Boolean")
return "false";
else
return "NULL";
}
export function mkType(libMap: LibMap, type: J.JTypeRef) {
var t = resolveTypeRef(libMap, type);
return mangleLibraryName(t.lib, t.type);
@ -87,6 +97,8 @@ module TDev {
if (outParams.length > 1)
throw new Error("Not supported (multiple return parameters)");
var retType = outParams.length ? mkType(libMap, outParams[0].type) : "void";
if (name == "main")
name = "app_main";
return [
retType, " ", name, "(",
inParams.map(p => mkParam(libMap, p)).join(", "),