2026-08-02 15:52:44 +10:00
|
|
|
#include "include/gravity/core.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2026-08-03 17:02:20 +10:00
|
|
|
int main(void) {
|
2026-08-04 10:10:49 +10:00
|
|
|
// define program
|
2026-08-02 15:52:44 +10:00
|
|
|
GravityModule module = gravityCreateModule();
|
2026-08-03 17:02:20 +10:00
|
|
|
GravityBuilder builder = gravityCreateBuilder();
|
|
|
|
|
|
2026-08-03 21:01:34 +10:00
|
|
|
GravityType charType = gravityCreateIntegerType(8, false);
|
2026-08-03 17:02:20 +10:00
|
|
|
|
|
|
|
|
GravityType mainFuncType = gravityCreateFunctionType(0, NULL, charType);
|
|
|
|
|
GravityFunction mainFunc = gravityNewFunction(module, "main", mainFuncType);
|
2026-08-04 09:50:48 +10:00
|
|
|
GravityBlock entryBlock = gravityStartBlock("entry", mainFunc);
|
2026-08-03 21:01:34 +10:00
|
|
|
|
2026-08-04 11:30:30 +10:00
|
|
|
/*
|
2026-08-04 09:50:48 +10:00
|
|
|
// define blocks
|
|
|
|
|
GravityBlock thenBlock = gravityStartBlock("then", mainFunc);
|
|
|
|
|
gravityAppendBlockArgument(thenBlock, ptrType);
|
2026-08-03 21:24:29 +10:00
|
|
|
|
2026-08-04 09:50:48 +10:00
|
|
|
GravityBlock elseBlock = gravityStartBlock("else", mainFunc);
|
|
|
|
|
gravityAppendBlockArgument(elseBlock, ptrType);
|
2026-08-03 21:24:29 +10:00
|
|
|
|
2026-08-04 09:50:48 +10:00
|
|
|
GravityBlock mergeBlock = gravityStartBlock("merge", mainFunc);
|
2026-08-04 10:10:49 +10:00
|
|
|
gravityAppendBlockArgument(mergeBlock, ptrType);
|
2026-08-04 09:50:48 +10:00
|
|
|
|
|
|
|
|
// write entry
|
|
|
|
|
gravityBuilderSwitchToBlock(builder, entryBlock);
|
|
|
|
|
GravityValue x = gravityBuildAlloc(builder, charType, "x");
|
|
|
|
|
|
2026-08-04 10:10:49 +10:00
|
|
|
GravityValue oneConst = gravityBuildIntConstant(builder, charType, 1, "true");
|
|
|
|
|
gravityBuildBr(builder, oneConst, thenBlock, (GravityValue[]){x}, elseBlock, (GravityValue[]){x});
|
2026-08-04 09:50:48 +10:00
|
|
|
|
|
|
|
|
// then block
|
|
|
|
|
gravityBuilderSwitchToBlock(builder, thenBlock);
|
|
|
|
|
GravityValue thenValue = gravityGetBlockArg(thenBlock, 0);
|
2026-08-04 10:10:49 +10:00
|
|
|
GravityValue fiveConst = gravityBuildIntConstant(builder, charType, 5, "five");
|
|
|
|
|
gravityBuildStore(builder, thenValue, fiveConst);
|
|
|
|
|
gravityBuildJump(builder, mergeBlock, (GravityValue[]){thenValue});
|
2026-08-04 09:50:48 +10:00
|
|
|
|
|
|
|
|
// else block
|
|
|
|
|
gravityBuilderSwitchToBlock(builder, elseBlock);
|
|
|
|
|
GravityValue elseValue = gravityGetBlockArg(elseBlock, 0);
|
2026-08-04 10:10:49 +10:00
|
|
|
GravityValue tenConst = gravityBuildIntConstant(builder, charType, 10, "ten");
|
|
|
|
|
gravityBuildStore(builder, elseValue, tenConst);
|
|
|
|
|
gravityBuildJump(builder, mergeBlock, (GravityValue[]){elseValue});
|
2026-08-04 09:50:48 +10:00
|
|
|
|
2026-08-04 10:10:49 +10:00
|
|
|
// merge block
|
2026-08-04 09:50:48 +10:00
|
|
|
gravityBuilderSwitchToBlock(builder, mergeBlock);
|
2026-08-04 10:10:49 +10:00
|
|
|
GravityValue xVar = gravityGetBlockArg(mergeBlock, 0);
|
|
|
|
|
GravityValue xValue = gravityBuildLoad(builder, xVar, "xValue");
|
2026-08-04 11:30:30 +10:00
|
|
|
gravityBuildRet(builder, xValue);*/
|
|
|
|
|
|
|
|
|
|
gravityBuilderSwitchToBlock(builder, entryBlock);
|
2026-08-05 19:17:15 +10:00
|
|
|
|
|
|
|
|
GravityValue variable = gravityBuildAlloc(builder, charType, NULL);
|
|
|
|
|
GravityValue returnVal = gravityNewIntConstant(charType, 123);//gravityBuildIntConstant(builder, charType, 123, NULL);
|
|
|
|
|
gravityBuildStore(builder, variable, returnVal);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GravityValue varValue = gravityBuildLoad(builder, variable, NULL);
|
|
|
|
|
gravityBuildRet(builder, varValue);
|
2026-08-03 21:01:34 +10:00
|
|
|
|
|
|
|
|
printf("%s\n", gravityFunctionToCStr(mainFunc));
|
2026-08-02 15:52:44 +10:00
|
|
|
|
2026-08-04 11:30:30 +10:00
|
|
|
GravityTargetArch target = gravityCreate_x86_64Target();
|
|
|
|
|
char* outputASM = gravityTargetEmitModule(target, module);
|
|
|
|
|
printf("%s\n", outputASM);
|
|
|
|
|
|
2026-08-02 15:52:44 +10:00
|
|
|
return 0;
|
|
|
|
|
}
|