Skip to content

Commit

Permalink
add <?if and <?findall blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Jan 13, 2023
1 parent 2215c85 commit 1a0b53e
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ifndef SPINBIN
SPINBIN = https://github.com/fermyon/spin/releases/download/$(SPINVER)/spin-$(SPINVER)-$(OS)-$(ARCH).tar.gz
endif

.PHONY: run watch container deploy
.PHONY: run watch container

all: run

Expand Down
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,36 @@ For example, this renders a table of the numbers 1-10 and their squares:
</table>
```

#### findall: `<?* Goal. ?>`
#### if block: `<?if Goal. ?> ... <?end ?>`
```html
<?if current_prolog_flag(dialect, X). ?>
You are using: <?=X ?>
<?end ?>
```

Conditionally executes the block if Goal succeeds. Only runs once.

#### findall block: `<?findall Goal. ?> ... <?end ?>`
```html
<table>
<?findall current_prolog_flag(Key, Value). ?>
<tr><td><?=Key ?></td><td><?=Value ?></td></tr>
<?end ?>
</table>
```

Works like if blocks, but with findall behavior.

#### findall query: `<?* Goal. ?>`
```prolog
<?* member(X, [1, 2, 3]) ?>
<?* member(X, [1, 2, 3]), write(X). ?>
```

Works the same as query, but findall behavior instead of once behavior.

#### Echo: `<?=Var Goal. ?>`
```html
1+1 = <?=X X is 1+1 ?>
1+1 = <?=X X is 1+1. ?>
<input type="text" name="ask" value="<?=Param query_param(ask, Param) ?>">
```

Expand All @@ -125,6 +146,8 @@ The web framework of the future is <?=Framework best_web_framework(Framework). ?

Assert facts and rules as if consulting a Prolog program. Directive syntax will call the given goal.

`<?prolog ... ?>` is an alias for this.

## API

### module(cgi)
Expand Down
2 changes: 1 addition & 1 deletion spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_version = "1"
name = "prolog-php"
description = "Prolog Home Page"
trigger = { type = "http", base = "/" }
version = "0.3.0"
version = "0.4.0"

[[component]]
id = "prolog"
Expand Down
128 changes: 106 additions & 22 deletions www/lib/php.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
php([H|T]) --> block(H), php(T).
php([]) --> [].

block(php(Head, Text)) --> "<?", seq(Head), whitespace, seq(Text), whitespace, "?>".
block(php(Head, [])) --> "<?", nonwhite(Head), whitespace, "?>".
block(php(Head, Text)) --> "<?", nonwhite(Head), whitespace, text(Text), whitespace, "?>".
block(php("prolog", Text)) --> "<?", whitespace, text(Text), whitespace, "?>".
block(text(Text)) --> text(Text), { Text \= [] }.

text(['<', X|T]) --> ['<', X], { dif(X, '?') }, text(T).
text([X|T]) --> [X], { dif(X, '<') }, text(T).
text([]) --> [].
text("<") --> "<".

nonwhite([X|Cs]) --> { dif(X, '\n'), dif(X, '\t'), dif(X, ' ') }, [X], { atom(X), \+char_type(X, white) }, nonwhite(Cs).
nonwhite([]) --> [].

whitespace --> [X], { atom(X), char_type(X, white) }, (whitespace | []).
whitespace --> " " | "\n" | "\t".

Expand All @@ -23,50 +28,91 @@
clauses([]) --> term(end_of_file).
term(T) --> read_term_from_chars_(T).

program([findall(Goal, Blocks)|Xs]) -->
[php("findall", Goal)],
program(Blocks),
[php("end", [])],
program(Xs).
program([if(Goal, Blocks)|Xs]) -->
[php("if", Goal)],
program(Blocks),
[php("end", [])],
program(Xs).
program([php(Head, Text)|Xs]) -->
{
dif(Head, "if"),
dif(Head, "findall"),
dif(Head, "end")
},
[php(Head, Text)],
program(Xs).
program([text(Text)|Xs]) -->
[text(Text)], program(Xs).
program([]) --> [].

exec(Block) :-
exec([], Block).

exec(Vars, Block) :-
\+unsafe_block(Block),
exec_flush,
nb_setval(capture, safe),
'$capture_output',
ignore(exec_(Block)),
ignore(exec_(Vars, Block)),
'$capture_output_to_chars'(Cs),
nb_delete(capture),
ignore(echo(Cs)), % escapes output
!.
exec(Block) :-
exec(Vars, Block) :-
unsafe_block(Block),
ignore(exec_(Block)).
exec_flush,
ignore(exec_(Vars, Block)).

exec_flush :-
catch(nb_getval(capture, safe), error(existence_error(_, _), _), fail),
'$capture_output_to_chars'(Cs),
ignore(echo(Cs)),
nb_delete(capture),
!.
exec_flush.

% <?- ... ?> (query)
% <?php ... ?>
exec_(php("-", Code)) :-
exec_(php("php", Code)),
exec_(Vars, php("-", Code)) :-
exec_(Vars, php("php", Code)),
!.
exec_(php("php", Code)) :-
read_term_from_chars(Code, Goal, []),
exec_(Vars0, php("php", Code)) :-
read_term_from_chars(Code, Goal, [variable_names(Vars1)]),
merge_vars(Vars0, Vars1, _),
ignore(Goal),
!.
exec_(php("unsafe", Code)) :-
exec_(php("php", Code)),
exec_(Vars, php("unsafe", Code)) :-
exec_(Vars, php("php", Code)),
!.
% <?* ... ?> (findall)
exec_(php("*", Code)) :-
read_term_from_chars(Code, Goal, []),
exec_(Vars0, php("*", Code)) :-
read_term_from_chars(Code, Goal, [variable_names(Vars1)]),
merge_vars(Vars0, Vars1, _),
ignore(findall(_, call(Goal), _)),
!.
% <?prolog ... ?> (clauses)
% <? ... ?>
exec_(php("prolog", Code)) :-
exec_(Vars, php("prolog", Code)) :-
( once(phrase(clauses(Cs), Code))
; throw(error(invalid_template(prolog, Code)))
),
ignore(maplist(prolog_call, Cs)),
!.
exec_(php([], Code)) :-
exec_(php("prolog", Code)),
exec_(Vars, php([], Code)) :-
exec_(Vars, php("prolog", Code)),
!.
% <?=Var ... ?> (echo)
exec_(php([=|Var], Code)) :-
read_term_from_chars(Code, Goal, [variable_names(Vars)]),
exec_(Vars0, php([=|Var], Code)) :-
Code \= [],
read_term_from_chars(Code, Goal, [variable_names(Vars1)]),
merge_vars(Vars0, Vars1, _),
atom_chars(Key, Var),
( member(Key=X, Vars)
( ( member(Key=X, Vars1) ; member(Key=X, Vars0) )
-> true
; throw(error(var_not_found(var(Key), goal(Goal))))
),
Expand All @@ -75,19 +121,57 @@
; true
),
!.
exec_(text(Text)) :-
exec_(Vars, php([=|Var], [])) :-
atom_chars(Key, Var),
( memberchk(Key=X, Vars)
-> true
; throw(error(var_not_found(var(Key))))
),
echo_unsafe(X),
!.
exec_(Vars0, if(Condition, Blocks)) :-
read_term_from_chars(Condition, Cond, [variable_names(Vars1)]),
merge_vars(Vars0, Vars1, Vars),
( call(Cond)
-> maplist(exec(Vars), Blocks)
; true
).
exec_(Vars0, findall(G, Blocks)) :-
read_term_from_chars(G, Goal, [variable_names(Vars1)]),
merge_vars(Vars0, Vars1, Vars),
findall(_, (call(Goal), maplist(exec(Vars), Blocks)), _).
exec_(_, text(Text)) :-
'$put_chars'(Text),
!.
exec_(X) :-
throw(error(unknown_block(X))).
exec_(Vars, X) :-
throw(error(unknown_block(X, vars(Vars)))).

unsafe_block(text(_)).
unsafe_block(php("unsafe", _)).

merge_vars(L0, L1) :-
maplist(merge_vars_(L1), L0).
merge_vars_(L, Name=Var) :-
( memberchk(Name=V1, L)
-> Var = V1
; true
).

merge_vars(Vs0, Vs1, Vs) :-
merge_vars(Vs0, Vs1),
merge_vars(Vs1, Vs0),
once(union(Vs0, Vs1, Vs)).

render(File) :-
read_file_to_string(File, Cs, []),
% write(Cs),
once(phrase(php(Program), Cs)),
once(phrase(php(PHP), Cs)),
( phrase(program(Program), PHP)
-> true
; throw(error(invalid_program(PHP)))
),
!,
logf("~nPHP: ~w~nProg: ~w~n", [PHP, Program]),
% write(Program).
catch(maplist(exec, Program), Error, (
logf("Script error: ~w~nCode: ~w~n", [Error, Program]),
Expand Down
33 changes: 10 additions & 23 deletions www/lib/phpinfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,29 @@
<header>
<h1>phpinfo/0<br>
<span style='font-size: 14pt;'>
Version: <?php pretty_version(Ver), write(Ver) ?>
Version: <?=Ver pretty_version(Ver). ?>
</span>
</h1>
</header>
<h3>Envrionment</h3>
<table>
<tr><th>Name</th><th>Value</th></tr>
<?unsafe
bagof([K, V], env(K, V), Env),
maplist(format("<tr><td>~w</td><td>~w</td></tr>"), Env)
?>
<?findall env(K, V). ?>
<tr><td><?=K ?></td><td><?=V ?></td></tr>
<?end ?>
</table>
<h3>Prolog flags</h3>
<table>
<tr><th>Flag</th><th>Value</th></tr>
<?unsafe
current_prolog_flag(argv, Argv),
format("<tr><td>~w</td><td>~w</td></tr>", [argv, Argv]),
current_prolog_flag(version_git, GitVer),
format("<tr><td>~w</td><td>~w</td></tr>", [version_git, GitVer]),
bagof([K, V], current_prolog_flag(K, V), Flags),
maplist(format("<tr><td>~w</td><td>~w</td></tr>"), Flags)
?>
<?findall current_prolog_flag(K, V). ?>
<tr><td><?=K ?></td><td><?=V ?></td></tr>
<?end ?>
</table>
<h3>Query params</h3>
<table>
<tr><th>Key</th><th>Value</th></tr>
<?unsafe
bagof([K, V], K0^V0^K1^V1^K^V^(
query_param(K0, V0),
atom_chars(K0, K1),
atom_chars(V0, V1),
html_escape(K1, K),
html_escape(V1, V)
), QueryParams),
maplist(format("<tr><td>~s</td><td>~s</td></tr>"), QueryParams)
?>
<?findall query_param(K, V). ?>
<tr><td><?=K ?></td><td><?=V ?></td></tr>
<?end ?>
</table>
</div>
6 changes: 3 additions & 3 deletions www/public_html/oracle.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ <h1>Oracle of Facts and Logic</h1>
</form>

<article class="qa">
<h3 class="q"><?=Ask query_param(ask, Ask) ?></h3>
<h3 class="a"><?=Wisdom query_param(ask, Ask), oracle(Ask, Wisdom) ?></h3>
<h3 class="q"><?=Ask query_param(ask, Ask). ?></h3>
<h3 class="a"><?=Wisdom query_param(ask, Ask), oracle(Ask, Wisdom). ?></h3>
</article>
</main>

<hr>

<section>
<h2>Source Code</h2>
<pre><?=Source current_file(File), read_file_to_string(File, Source, []) ?></pre>
<pre><?=Source current_file(File), read_file_to_string(File, Source, []). ?></pre>
</section>

<hr>
Expand Down
5 changes: 3 additions & 2 deletions www/public_html/tacos.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ <h1>So I made this girl quit her job today.</h1>
?>

<ol>
<!-- TODO: make this less horrible -->
<?unsafe findall(_, (tacos(Tacos), member(taco(Shell, Meat, Toppings), Tacos), format("<li>~a ~a ~w</li>", [Shell, Meat, Toppings])), _). ?>
<?findall tacos(Tacos), member(taco(Shell, Meat, Toppings), Tacos). ?>
<li><?=Shell ?> shell <?=Meat ?> <?=Toppings ?></li>
<?end ?>
</ol>

<section>
Expand Down
2 changes: 1 addition & 1 deletion www/public_html/test.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<html>
<?unsafe phpinfo ?>
<?unsafe phpinfo. ?>
</html>

0 comments on commit 1a0b53e

Please sign in to comment.