Permalink
Browse files

Can now serialize osh.asdl to oheap!

C++ code to print more of the osh.asdl tree.  This could be generated.
  • Loading branch information...
Andy Chu
Andy Chu committed Nov 26, 2017
1 parent cd38f4a commit 86b8f789a93a23fe66578a2decda261cb07d286e
Showing with 86 additions and 12 deletions.
  1. +2 −5 asdl/encode.py
  2. +79 −4 asdl/osh_demo.cc
  3. +5 −3 asdl/run.sh
View
@@ -74,6 +74,8 @@ def Tag(self, i, chunk):
chunk.append(i & 0xFF)
def Int(self, n, chunk):
if n < 0:
raise Error('ASDL currently supports unsigned integers, got %d' % n)
if n > self.max_int:
raise Error('%d is too big to fit in %d bytes' % (n, self.int_width))
@@ -195,17 +197,12 @@ def EncodeObj(obj, enc, out):
for name in obj.FIELDS: # encode in order
desc = obj.DESCRIPTOR_LOOKUP[name]
#print('\n\n------------')
#print('field DESC', name, desc)
field_val = getattr(obj, name)
#print('VALUE', field_val)
# TODO:
# - Float would be inline, etc.
# - Repeated value: write them all adjacent to each other?
is_maybe = False
if isinstance(desc, asdl.MaybeType):
is_maybe = True
View
@@ -10,9 +10,84 @@ int GetRootRef(uint8_t* image) {
if (image[0] != 'O') return -1;
if (image[1] != 'H') return -1;
if (image[2] != 'P') return -1;
if (image[3] != 4) return -1;
if (image[3] != 1) return -1; // version 1
if (image[4] != 4) return -1; // alignment 4
return image[4] + (image[5] << 8) + (image[6] << 16) + (image[7] << 24);
return image[5] + (image[6] << 8) + (image[7] << 16);
}
void PrintToken(const uint32_t* base, const token_t& e, int indent) {
for (int i = 0; i < indent; ++i) {
putchar('\t');
}
printf("Token %hhu %s\n", e.id(), e.val(base));
}
void PrintWordPart(const uint32_t* base, const word_part_t& e, int indent) {
for (int i = 0; i < indent; ++i) {
putchar('\t');
}
printf("t%hhu ", e.tag());
switch (e.tag()) {
case word_part_e::LiteralPart: {
auto& e2 = static_cast<const LiteralPart&>(e);
printf("LiteralPart\n");
PrintToken(base, e2.token(base), indent+1);
break;
}
default:
printf("OTHER word_part\n");
break;
}
}
void PrintWord(const uint32_t* base, const word_t& e, int indent) {
for (int i = 0; i < indent; ++i) {
putchar('\t');
}
printf("t%hhu ", e.tag());
switch (e.tag()) {
case word_e::CompoundWord: {
auto& e2 = static_cast<const CompoundWord&>(e);
printf("CompoundWord %d\n", e2.parts_size(base));
for (int i = 0; i < e2.parts_size(base); ++i) {
PrintWordPart(base, e2.parts(base, i), indent+1);
}
break;
}
default:
printf("OTHER word\n");
break;
}
}
void PrintCommand(const uint32_t* base, const command_t& e, int indent) {
for (int i = 0; i < indent; ++i) {
putchar('\t');
}
printf("t%hhu ", e.tag());
switch (e.tag()) {
case command_e::SimpleCommand: {
auto& e2 = static_cast<const SimpleCommand&>(e);
printf("SimpleCommand %d\n", e2.words_size(base));
for (int i = 0; i < e2.words_size(base); ++i) {
PrintWord(base, e2.words(base, i), indent+1);
}
break;
}
case command_e::CommandList: {
auto& e2 = static_cast<const CommandList&>(e);
printf("CommandList %d\n", e2.children_size(base));
for (int i = 0; i < e2.children_size(base); ++i) {
PrintCommand(base, e2.children(base, i), indent+1);
}
break;
}
default:
printf("OTHER\n");
break;
}
}
int main(int argc, char **argv) {
@@ -48,6 +123,6 @@ int main(int argc, char **argv) {
auto base = reinterpret_cast<uint32_t*>(image);
size_t offset = alignment * root_ref;
auto expr = reinterpret_cast<arith_expr_t*>(image + offset);
//PrintExpr(base, *expr, 0);
auto expr = reinterpret_cast<command_t*>(image + offset);
PrintCommand(base, *expr, 0);
}
View
@@ -98,7 +98,7 @@ arith-demo() {
local data=_tmp/${name}.bin
# Write a binary
asdl-arith-encode '7 + 9' $data
asdl-arith-encode '7 * 9' $data
local bin=_tmp/${name}_demo
@@ -115,8 +115,10 @@ osh-demo() {
local name=osh
local data=_tmp/${name}.bin
# TODO: Fix bug -- this is only 3 bytes?
bin/osh -n --ast-format oheap -c 'echo hi # comment' > $data
local code='echo hi; echo bye # comment'
#local code='echo $(( 2 + 3 ))'
#local code='echo $(( -2 * -3 ))' # test negative integers
bin/osh -n --ast-format oheap -c "$code" > $data
ls -l $data

0 comments on commit 86b8f78

Please sign in to comment.