Skip to content

Commit

Permalink
Improved contributor source editing experience by improved code gener…
Browse files Browse the repository at this point in the history
…ator markers.

I suddenly realized today that I could eliminate lots of red wavy
lines in the source editor when working on generate/template/astronomy.*
files, by tweaking the $ASTRO_... tokens to look like comments.
For some reason, I didn't think of doing this before now.
I was motivated by the Go language support, where the tokens were
breaking `go fmt`.
  • Loading branch information
cosinekitty committed Oct 5, 2023
1 parent 520a532 commit 2915cd9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 44 deletions.
18 changes: 15 additions & 3 deletions generate/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ static int IndexOf(const char *text, int offset, const char *pattern)
return -1;
}

static int ScanDirective(
static int ScanDirectiveVariant(
char *line,
const char **verb_text,
const char **args_text,
const char **tail_text)
const char **tail_text,
const char *prefix)
{
static const char prefix[] = "$ASTRO_";
static const char delim[] = "(";
static const char suffix[] = ")";
int prefix_length = (int)strlen(prefix);
Expand Down Expand Up @@ -198,6 +198,18 @@ static int ScanDirective(
return 1;
}

static int ScanDirective(
char *line,
const char **verb_text,
const char **args_text,
const char **tail_text)
{
return
ScanDirectiveVariant(line, verb_text, args_text, tail_text, "//$ASTRO_") ||
ScanDirectiveVariant(line, verb_text, args_text, tail_text, "#$ASTRO_") ||
ScanDirectiveVariant(line, verb_text, args_text, tail_text, "$ASTRO_");
}

static int ParseVsopBodyName(const cg_context_t *context, const char *name, vsop_body_t *body)
{
if (!strcmp(name, "Sun")) { *body = VSOP_SUN; return 0; }
Expand Down
26 changes: 13 additions & 13 deletions generate/template/astronomy.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extern "C" {
/** @cond DOXYGEN_SKIP */
#define PI 3.14159265358979323846

$ASTRO_C_PLUTO_CONST()
//$ASTRO_C_PLUTO_CONST()

typedef enum
{
Expand Down Expand Up @@ -2133,7 +2133,7 @@ static void CalcMoon(

context.t = centuries_since_j2000;
Init(ctx);
$ASTRO_ADDSOL()
//$ASTRO_ADDSOL()
SolarN(ctx);
Planetary(ctx);
S = F + DS/ARC;
Expand Down Expand Up @@ -2536,14 +2536,14 @@ typedef struct
jupiter_moon_t;
/** @endcond */

$ASTRO_C_VSOP(Mercury);
$ASTRO_C_VSOP(Venus);
$ASTRO_C_VSOP(Earth);
$ASTRO_C_VSOP(Mars);
$ASTRO_C_VSOP(Jupiter);
$ASTRO_C_VSOP(Saturn);
$ASTRO_C_VSOP(Uranus);
$ASTRO_C_VSOP(Neptune);
//$ASTRO_C_VSOP(Mercury);
//$ASTRO_C_VSOP(Venus);
//$ASTRO_C_VSOP(Earth);
//$ASTRO_C_VSOP(Mars);
//$ASTRO_C_VSOP(Jupiter);
//$ASTRO_C_VSOP(Saturn);
//$ASTRO_C_VSOP(Uranus);
//$ASTRO_C_VSOP(Neptune);

/** @cond DOXYGEN_SKIP */
#define VSOPFORMULA(x) { ASTRO_ARRAYSIZE(x), x }
Expand Down Expand Up @@ -3544,7 +3544,7 @@ void Astronomy_GravSimFree(astro_grav_sim_t *sim)

/*------------------ begin Pluto integrator ------------------*/

$ASTRO_PLUTO_TABLE();
//$ASTRO_PLUTO_TABLE();

/* FIXFIXFIX - Using a global is not thread-safe. Either add thread-locks or change API to accept a cache pointer. */
static body_segment_t *pluto_cache[PLUTO_NUM_STATES-1];
Expand Down Expand Up @@ -3707,7 +3707,7 @@ static astro_status_t CalcPluto(body_state_t *bstate, astro_time_t time, int hel

/*---------------------- begin Jupiter moons ----------------------*/

$ASTRO_JUPITER_MOONS();
//$ASTRO_JUPITER_MOONS();

static astro_state_vector_t JupiterMoon_elem2pv(astro_time_t time, double mu, const double elem[6])
{
Expand Down Expand Up @@ -9505,7 +9505,7 @@ typedef struct
constel_boundary_t;
/** @endcond */

$ASTRO_CONSTEL()
//$ASTRO_CONSTEL()

/**
* @brief
Expand Down
24 changes: 12 additions & 12 deletions generate/template/astronomy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ internal MoonContext(double centuries_since_j2000)
internal MoonResult CalcMoon()
{
++Astronomy.CalcMoonCount;
$ASTRO_ADDSOL()
//$ASTRO_ADDSOL()
SolarN();
Planetary();
S = F + DS/Astronomy.ARC;
Expand Down Expand Up @@ -3209,14 +3209,14 @@ public vsop_model_t(vsop_series_t[] lon, vsop_series_t[] lat, vsop_series_t[] ra
}
};

$ASTRO_CSHARP_VSOP(Mercury)
$ASTRO_CSHARP_VSOP(Venus)
$ASTRO_CSHARP_VSOP(Earth)
$ASTRO_CSHARP_VSOP(Mars)
$ASTRO_CSHARP_VSOP(Jupiter)
$ASTRO_CSHARP_VSOP(Saturn)
$ASTRO_CSHARP_VSOP(Uranus)
$ASTRO_CSHARP_VSOP(Neptune)
//$ASTRO_CSHARP_VSOP(Mercury)
//$ASTRO_CSHARP_VSOP(Venus)
//$ASTRO_CSHARP_VSOP(Earth)
//$ASTRO_CSHARP_VSOP(Mars)
//$ASTRO_CSHARP_VSOP(Jupiter)
//$ASTRO_CSHARP_VSOP(Saturn)
//$ASTRO_CSHARP_VSOP(Uranus)
//$ASTRO_CSHARP_VSOP(Neptune)

private static readonly vsop_model_t[] vsop = new vsop_model_t[]
{
Expand Down Expand Up @@ -3525,7 +3525,7 @@ private static body_state_t CalcVsopPosVel(vsop_model_t model, double tt)

#region Pluto

$ASTRO_PLUTO_TABLE();
//$ASTRO_PLUTO_TABLE();

internal static TerseVector UpdatePosition(double dt, TerseVector r, TerseVector v, TerseVector a)
{
Expand Down Expand Up @@ -3753,7 +3753,7 @@ private struct jupiter_moon_t
public vsop_term_t[] zeta;
}

$ASTRO_JUPITER_MOONS();
//$ASTRO_JUPITER_MOONS();

private static StateVector JupiterMoon_elem2pv(
AstroTime time,
Expand Down Expand Up @@ -10583,7 +10583,7 @@ public static ConstellationInfo Constellation(double ra, double dec)
throw new InternalError($"Unable to find constellation for coordinates: RA={ra}, DEC={dec}");
}

$ASTRO_CONSTEL()
//$ASTRO_CONSTEL()

}
}
24 changes: 12 additions & 12 deletions generate/template/astronomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ def Frac(x: float) -> float:

I += 1

$ASTRO_ADDSOL()
#$ASTRO_ADDSOL()

def ADDN(coeffn: float, p: int, q: int, r: int, s: int) -> float:
return coeffn * (ex[p][1] * ex[q][2] * ex[r][3] * ex[s][4]).imag
Expand Down Expand Up @@ -1804,14 +1804,14 @@ def __init__(self, lon: _vsop_formula_t, lat: _vsop_formula_t, rad: _vsop_formul
self.rad = rad

_vsop: List[_vsop_model_t] = [
$ASTRO_PYTHON_VSOP(Mercury),
$ASTRO_PYTHON_VSOP(Venus),
$ASTRO_PYTHON_VSOP(Earth),
$ASTRO_PYTHON_VSOP(Mars),
$ASTRO_PYTHON_VSOP(Jupiter),
$ASTRO_PYTHON_VSOP(Saturn),
$ASTRO_PYTHON_VSOP(Uranus),
$ASTRO_PYTHON_VSOP(Neptune),
#$ASTRO_PYTHON_VSOP(Mercury),
#$ASTRO_PYTHON_VSOP(Venus),
#$ASTRO_PYTHON_VSOP(Earth),
#$ASTRO_PYTHON_VSOP(Mars),
#$ASTRO_PYTHON_VSOP(Jupiter),
#$ASTRO_PYTHON_VSOP(Saturn),
#$ASTRO_PYTHON_VSOP(Uranus),
#$ASTRO_PYTHON_VSOP(Neptune),
]

def _VsopFormula(formula: _vsop_formula_t, t: float, clamp_angle: bool) -> float:
Expand Down Expand Up @@ -1968,7 +1968,7 @@ def __init__(self, tt: float, pos: Tuple[float, float, float], vel: Tuple[float,
self.pos = _TerseVector(pos[0], pos[1], pos[2])
self.vel = _TerseVector(vel[0], vel[1], vel[2])

$ASTRO_PLUTO_TABLE()
#$ASTRO_PLUTO_TABLE()

def _AdjustBarycenterPosVel(ssb: _body_state_t, tt: float, body: Body, planet_gm: float) -> _body_state_t:
shift = planet_gm / (planet_gm + _SUN_GM)
Expand Down Expand Up @@ -2205,7 +2205,7 @@ def __init__(self, mu: float, al0: float, al1: float, a: _js, l: _js, z: _js, ze
self.z = z
self.zeta = zeta

$ASTRO_JUPITER_MOONS()
#$ASTRO_JUPITER_MOONS()

class JupiterMoonsInfo:
"""Holds the positions and velocities of Jupiter's major 4 moons.
Expand Down Expand Up @@ -6438,7 +6438,7 @@ def __repr__(self) -> str:

_ConstelRot = None
_Epoch2000 = None
$ASTRO_CONSTEL()
#$ASTRO_CONSTEL()


def Constellation(ra: float, dec: float) -> ConstellationInfo:
Expand Down
8 changes: 4 additions & 4 deletions generate/template/astronomy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ function CalcMoon(time: AstroTime) {
SINPI += coeffp * result.x;
}

$ASTRO_ADDSOL()
//$ASTRO_ADDSOL()

function ADDN(coeffn: number, p: number, q: number, r: number, s: number) {
return coeffn * Term(p, q, r, s).y;
Expand Down Expand Up @@ -2596,7 +2596,7 @@ function CalcSolarSystemBarycenter(time: AstroTime): Vector {

// Pluto integrator begins ----------------------------------------------------

$ASTRO_PLUTO_TABLE()
//$ASTRO_PLUTO_TABLE()

class TerseVector {
constructor(
Expand Down Expand Up @@ -2958,7 +2958,7 @@ interface jupiter_moon_t {
zeta: jm_series_t;
};

$ASTRO_JUPITER_MOONS();
//$ASTRO_JUPITER_MOONS();

/**
* @brief Holds the positions and velocities of Jupiter's major 4 moons.
Expand Down Expand Up @@ -6836,7 +6836,7 @@ export function Rotation_GAL_EQJ(): RotationMatrix {
}


$ASTRO_CONSTEL()
//$ASTRO_CONSTEL()

let ConstelRot: RotationMatrix;
let Epoch2000 : AstroTime;
Expand Down

0 comments on commit 2915cd9

Please sign in to comment.