CIR Playground
Load example...
CIR on GitHub
Input
Run
// @bf() macro that uses CirQ/CirAQ quote-antiquote library #include <cir.h> #include <cirq.h> #include <stdio.h> static CirCodeId bf(const char *code) { return @CirQ(({ char mem[3000]; char *ptr = mem; memset(mem, 0, 3000); @CirAQ(({ CirStmtId jumpStack[100]; unsigned jumpStackLen = 0; CirCodeId outCodeId = CirCode_ofExpr(NULL); while (*code) { char c = *code; int count; if (c == '>') { for (count = 0; *code == '>'; count = count + 1, code = code + 1); CirCode_append(outCodeId, @CirQ(ptr = ptr + @CirAQ(count))); } else if (c == '<') { for (count = 0; *code == '<'; count = count + 1, code = code + 1); CirCode_append(outCodeId, @CirQ(ptr = ptr - @CirAQ(count))); } else if (c == '+') { for (count = 0; *code == '+'; count = count + 1, code = code + 1); CirCode_append(outCodeId, @CirQ(*ptr = *ptr + @CirAQ(count))); } else if (c == '-') { for (count = 0; *code == '-'; count = count + 1, code = code + 1); CirCode_append(outCodeId, @CirQ(*ptr = *ptr - @CirAQ(count))); } else if (c == '.') { CirCode_append(outCodeId, @CirQ(putchar(*ptr))); code = code + 1; } else if (c == ',') { CirCode_append(outCodeId, @CirQ(*ptr = getchar())); code = code + 1; } else if (c == '[') { // Cmp (we don't know jump target yet) CirStmtId stmtId = CirCode_appendNewStmt(outCodeId); CirStmt_toCmp(stmtId, CIR_CONDOP_EQ, @CirM_value(*ptr), @CirM_value(0), 0); jumpStack[jumpStackLen++] = stmtId; ++code; } else if (c == ']') { // Backpatch CirStmtId targetJump = jumpStack[--jumpStackLen]; CirStmtId stmtId = CirCode_appendNewStmt(outCodeId); CirStmt_toGoto(stmtId, targetJump); stmtId = CirCode_appendNewStmt(outCodeId); CirStmt_setJumpTarget(targetJump, stmtId); ++code; } else { ++code; } } outCodeId; })); })); } int main(void) { // Hello World @bf("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."); return 0; }
stdout
stderr
Load Example...
×
00 - Compile-time fibonacci
01 - Inline Repeat
02 - Compile-time strtok
03 - Does a code call a function
04 - C Preprocessor identifiers may be accidentally shadowed
05 - CIR identifiers won't be accidentally shadowed
06 - C Preprocessor accidental capture of symbols
07 - CIR won't accidentally capture symbols
08 - bf-to-C (low level ver)
09 - bf-to-C (with quote-antiquote)
10 - async-await
11 - Embed an external file
12 - zlib