Skip to content

Commit

Permalink
implement assign int=string and string=int, half-implement builtins d…
Browse files Browse the repository at this point in the history
…ie and downcase
  • Loading branch information
NotFound committed Oct 26, 2011
1 parent a9ae57b commit 4636d81
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
13 changes: 13 additions & 0 deletions t/base/features.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,26 @@ function main [main] (args)
int i = 1;
test.ok(i == 1, "int initialization");

string str = "hi";
test.is_string(str, "hi", "string initialization");
str = "hello";
test.is_string(str, "hello", "string assign");
str = i;
test.is_string(str, "1", "string assign from int");
str = 42;
i = str;
test.is(i, 42, "int assign from string");
i = "44";
test.is(i, 44, "int assign from string literal");

var n = null;
test.ok(n == null, "var null initialization");

var vi1 = 42;
test.is(vi1, 42, "var initialized with int");
test.ok(vi1 != null, "var initialized with int is not null");

i = 1;
test.is(i == 1 ? vi1 : 44, 42, "conditional operator var - int");
test.is(i != 1 ? 44 : vi1, 42, "conditional operator int - var");

Expand Down
31 changes: 28 additions & 3 deletions winxedxx.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ function emit_callbuiltinexpr(out, expr)
case 'join':
emit_callbuiltin_simple_2(out, "wxx_join", arg0, arg1);
break;

case 'downcase':
// Fake implementation, just copy the argument
emit_expr(out, arg0);
break;

case 'push':
emit_expr(out, arg0);
out.print(".push");
Expand All @@ -406,6 +412,11 @@ function emit_callbuiltinexpr(out, expr)
case 'split':
emit_callbuiltin_simple_2(out, "wxx_split", arg0, arg1);
break;
case 'die':
out.print("wxx_error(");
emit_expr(out, arg0);
out.print(")");
break;
case 'Error':
out.print("wxx_error(");
emit_expr(out, arg0);
Expand Down Expand Up @@ -468,6 +479,7 @@ function emit_callbuiltinexpr(out, expr)
case 'load_bytecode':
case 'load_language':
case 'compreg':
case 'getinterp':
out.print("winxedxxnull");
break;

Expand Down Expand Up @@ -624,12 +636,25 @@ function emit_assignexpr(out, assignexpr)
{
var lexpr = assignexpr.lexpr;
var rexpr = assignexpr.rexpr;
string ltype = lexpr.checkresult();
string rtype = rexpr.checkresult();
if (lexpr instanceof IdentifierExpr) {
out.print(lexpr.name);
out.print(" = ");
// Coerce result type to avoid ambiguities
out.print(generatedType(lexpr.checkresult()));
emit_expr(out, rexpr);
switch {
case ltype == REGstring && rtype == REGint:
out.print('wxx_int_to_string');
emit_expr(out, rexpr);
break;
case ltype == REGint && rtype == REGstring:
out.print('wxx_string_to_int');
emit_expr(out, rexpr);
break;
default:
// Coerce result type to avoid ambiguities
out.print(generatedType(lexpr.checkresult()));
emit_expr(out, rexpr);
}
}
else if (lexpr instanceof MemberExpr) {
emit_expr(out, lexpr.left);
Expand Down
2 changes: 2 additions & 0 deletions winxedxx_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace WinxedXX

std::string wxx_int_to_string(int i);
std::string wxx_num_to_string(double value);
int wxx_string_to_int(const std::string &src);
int wxx_string_to_int(const char *src);
std::string wxx_repeat_string(std::string s, int n);

class WxxInteger : public WxxDefault
Expand Down
12 changes: 12 additions & 0 deletions winxedxx_util.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <sstream>

#include <stdlib.h>

#include <sys/types.h>
#include <sys/wait.h>

Expand Down Expand Up @@ -62,6 +64,16 @@ std::string wxx_num_to_string(double value)
return oss.str();
}

int wxx_string_to_int(const std::string &src)
{
return atoi(src.c_str());
}

int wxx_string_to_int(const char *src)
{
return atoi(src);
}

std::string wxx_repeat_string(std::string s, int n)
{
std::string result;
Expand Down

0 comments on commit 4636d81

Please sign in to comment.