cmd/land: support stdout, fizzbuzz

This commit is contained in:
Cadey Ratio 2018-06-21 18:43:03 -07:00
parent 00c3cbf4d7
commit 3932cd5f07
3 changed files with 80 additions and 0 deletions

View File

@ -227,6 +227,11 @@ func (p *Process) open(fnamesP int32, flags int32) int32 {
var err error var err error
switch str { switch str {
case "stdout": case "stdout":
fi = stdFD{
ReadWriteCloser: os.Stdout,
}
case "log":
fi = stdFD{ fi = stdFD{
ReadWriteCloser: loggerFile{ ReadWriteCloser: loggerFile{
Logger: log.New(os.Stdout, p.name+" ", log.LstdFlags), Logger: log.New(os.Stdout, p.name+" ", log.LstdFlags),

View File

@ -116,3 +116,7 @@ func TestStruct(t *testing.T) {
func TestCHelloWorld(t *testing.T) { func TestCHelloWorld(t *testing.T) {
testWasmFile(t, "./testdata/helloworld.wasm") testWasmFile(t, "./testdata/helloworld.wasm")
} }
func TestFizzbuzz(t *testing.T) {
testWasmFile(t, "./testdata/fizzbuzz.wasm")
}

71
cmd/land/testdata/fizzbuzz.c vendored Normal file
View File

@ -0,0 +1,71 @@
extern int open(const char *, int);
extern int write(int, char *, int);
char intToLetter(int i) {
switch (i) {
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
}
return '?';
}
char* makeIntString(int i) {
char a[3];
int hundreds = i / 100;
int tens = (i/10) % 10;
int ones = i % 10;
a[0] = intToLetter(hundreds);
a[1] = intToLetter(tens);
a[2] = intToLetter(ones);
return a;
}
__attribute__ ((visibility ("default")))
int main() {
int fd;
int x;
fd = open("stdout", 0x42);
for (x = 0; x < 100; x++) {
int wrote = 0;
if (x % 3 == 0) {
wrote = 1;
write(fd, "Fizz", 4);
}
if (x % 5 == 0) {
wrote = 1;
write(fd, "Buzz", 4);
}
if (wrote == 0) {
write(fd, makeIntString(x), 3);
}
write(fd, "\n", 1);
}
return 0;
}