~yerinalexey/hare-annotate

A library for code generators in Hare
Use os::args[0] in 'generated by' boilerplate
Fix example/Makefile HAREPATH resolution
Allow returning void from decl_generator to ignore a declaration

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~yerinalexey/hare-annotate
read/write
git@git.sr.ht:~yerinalexey/hare-annotate

You can also use your local clone with git send-email.

#hare-annotate

Generator of code generators for Hare.

annotate is a library that helps to write code generators in Hare with a standard notation of @name.

#Installation

#System-wide installation

$ make install

#Vendoring

$ git subtree -P vendor/hare-annotate/ add https://git.sr.ht/~yerinalexey/hare-annotate master

#Simple generators

This basic generator replaces @pi with the value of π:

use annotate::ast;
use annotate::lex;
use annotate::parse;
use annotate;
use math;
use os;

fn gen_pi(lexer: *lex::lexer) (ast::expr | parse::error) = {
	return annotate::mkexpr(ast::number_constant {
		suff = lex::ltok::LIT_FCONST,
		value = math::PI,
		sign = false,
	});
};

export fn main() void = {
	const annotator = annotate::init(os::stdin, "<stdin>");
	annotate::register(&annotator, "@pi", &gen_pi);
	annotate::generate(&annotator, os::stdout)!;
};

It takes source from standard input and writes the result to standard output:

$ cat test.ha
let pi: f32 = @pi;
$ hare run generator.ha < test.ha
// Generated using generator.ha, DO NOT EDIT!
let pi: f32 = 3.141593;

Just note that @pi does not behave like a number constant, and must be wrapped in () to be used in arithmetic expressions. Use (@pi) * 2 instead of @pi * 2.

The gen_pi function (called a generator) can also freely use the lexer provided to it. See example/generator.ha for an example of what can be done with it.

#Contributing

Please send patches to ~yerinalexey/public-inbox@lists.sr.ht using git send-email with prefix set to hare-annotate:

$ git config format.subjectPrefix "PATCH hare-annotate"