Browse Source

Fixed Writer formatting

Both K&R One True Brace and Allman are looking pretty good now
HotReloadingState
Macoy Madson 2 years ago
parent
commit
5310697d3f
  1. 3
      runtime/HotReloading.cake
  2. 4
      runtime/TextAdventure.cake
  3. 4
      src/Generators.cpp
  4. 44
      src/Writer.cpp

3
runtime/HotReloading.cake

@ -34,12 +34,13 @@
(unless current-lib
(return false))
(for-in function-referent-it FunctionReferenceMapPair registered-functions
(for-in function-referent-it (& FunctionReferenceMapPair) registered-functions
(var loaded-symbol (* void)
(getSymbolFromDynamicLibrary current-lib
(on-call (path function-referent-it . first) c_str)))
(unless loaded-symbol
(return false))
;; TODO: What will happen once modules are unloaded? We can't store pointers to their static memory
(for-in function-pointer (* (* void)) (path function-referent-it . second)
(set (deref function-pointer) loaded-symbol)))
(return true))

4
runtime/TextAdventure.cake

@ -1,3 +1,7 @@
;; Cake Adventure
;; This is meant to be a dead simple "game" which you can modify while it is running.
;; This is to test hot-reloading
;; (import &comptime-only "Macros.cake")
(c-import "<stdio.h>"

4
src/Generators.cpp

@ -934,9 +934,9 @@ static void tokenizeGenerateStringTokenize(const char* outputVarName, const Toke
// TODO Need to gensym error, or replace with a function call
char tokenizeLineBuffer[2048] = {0};
PrintfBuffer(tokenizeLineBuffer,
"if (!tokenizeLinePrintError(\"%s\", \"%s\", %d, %s)) {return false;}\n",
"if (!tokenizeLinePrintError(\"%s\", \"%s\", %d, %s)) {return false;}",
stringToTokenize, triggerToken.source, triggerToken.lineNumber, outputVarName);
addStringOutput(output.source, tokenizeLineBuffer, StringOutMod_None, &triggerToken);
addStringOutput(output.source, tokenizeLineBuffer, StringOutMod_NewlineAfter, &triggerToken);
}
bool TokenizePushGenerator(EvaluatorEnvironment& environment, const EvaluatorContext& context,

44
src/Writer.cpp

@ -165,6 +165,7 @@ struct StringOutputState
// For determining character offsets to pass to e.g. Emacs' goto-char
int numCharsOutput;
int currentLine;
int lastLineIndented;
FILE* fileOut;
};
@ -186,8 +187,13 @@ static void Writer_Writef(StringOutputState& state, const char* format, ...)
static void printIndentation(const WriterFormatSettings& formatSettings, StringOutputState& state)
{
// TODO: Good indentation
return;
// Only indent at beginning of line. This will of course break if there are newlines added in
// strings being output
if (state.currentLine == state.lastLineIndented)
return;
state.lastLineIndented = state.currentLine;
if (formatSettings.indentStyle == WriterFormatIndentType_Tabs)
{
for (int i = 0; i < state.blockDepth; ++i)
@ -205,12 +211,9 @@ static void printIndentation(const WriterFormatSettings& formatSettings, StringO
}
}
static void writeStringOutput(const NameStyleSettings& nameSettings,
const WriterFormatSettings& formatSettings,
const StringOutput& outputOperation, StringOutputState& state)
static void updateDepthDoIndentation(const WriterFormatSettings& formatSettings,
const StringOutput& outputOperation, StringOutputState& state)
{
// First, handle indentation
printIndentation(formatSettings, state);
if (outputOperation.modifiers & StringOutMod_OpenBlock)
{
state.blockDepth++;
@ -225,6 +228,15 @@ static void writeStringOutput(const NameStyleSettings& nameSettings,
}
}
printIndentation(formatSettings, state);
}
static void writeStringOutput(const NameStyleSettings& nameSettings,
const WriterFormatSettings& formatSettings,
const StringOutput& outputOperation, StringOutputState& state)
{
updateDepthDoIndentation(formatSettings, outputOperation, state);
if (outputOperation.modifiers & StringOutMod_SpaceBefore)
Writer_Writef(state, " ");
@ -246,12 +258,22 @@ static void writeStringOutput(const NameStyleSettings& nameSettings,
Writer_Writef(state, "{");
else if (formatSettings.braceStyle == WriterFormatBraceStyle_Allman)
{
Writer_Writef(state, "\n{\n");
state.currentLine += 2;
Writer_Writef(state, "\n");
state.currentLine += 1;
// Allman brackets are not as deep as their contents, but this bracket was already
// counted by updateDepthDoIndentation() above. Take it back one so indentation is
// correct for the bracket, then go back to the proper block indentation
state.blockDepth -= 1;
printIndentation(formatSettings, state);
state.blockDepth += 1;
Writer_Writef(state, "{\n");
state.currentLine += 1;
}
else if (formatSettings.braceStyle == WriterFormatBraceStyle_KandR_1TBS)
{
Writer_Writef(state, "{\n");
Writer_Writef(state, " {\n");
state.currentLine += 1;
}
}
@ -283,7 +305,7 @@ static void writeStringOutput(const NameStyleSettings& nameSettings,
++state.currentLine;
}
printIndentation(formatSettings, state);
// printIndentation(/*isCloseBlockLine=*/false, formatSettings, state);
}
else if (outputOperation.modifiers & StringOutMod_ListSeparator)
Writer_Writef(state, ", ");

Loading…
Cancel
Save