Giter Site home page Giter Site logo

php's Introduction

php: Prolog Home Page

This is an experiment using the WebAssembly version of Trealla Prolog and Spin to host websites. It is a retrofuturistic combination of Prolog/C and Rust/WebAssembly.

Status: Slowly approaching stability? Still in cowboy mode. ๐Ÿค 

Demo

Head on over to our beautiful homepage at php.energy. Source code examples included.

Setup

  1. Reconsider whether you really want to do this.
  2. Install Spin.
  3. For hot reloading, install nodemon (optional).
  4. Clone this repo.
  5. Configure the www root in spin.toml
  6. Put PHP scripts or Prolog programs in public_html.

Run

  • Run server with make or spin up.
  • Or use make watch for hot reloading.

Deploy to ~the cloud~ ๐Ÿ†•

As of recently, you can deploy to the beta version of Fermyon cloud for free. Neat. More info here.

  1. Get set up
  2. spin deploy

Container Build

See: Spin docs on OCI images

How does it work?

It just runs the WebAssembly version of Trealla Prolog and writes CGI (RFC 3875) output to stdout.

See the README in the www folder for more info on the file structure.

Currently this uses a fork of Trealla hosted on WAPM, but it works with upstream Trealla too.

What's next?

This currently uses the CGI mode of Spin, but with a little bit of effort we could use the fancy APIs and get outgoing HTTP and Redis and whatnot. It'd be cool to get some kind of magic persistence going.

File Layout

public_html

Put PHP templates here and they will show up in your root.

Templates

Files ending in .html will be interpreted as PHP templates with the following special syntax.

Queries: <?- Goal. ?>

<?- current_prolog_flag(version, Ver), write(Ver) ?>
<?php ... ?>

You can use the <?- Goal. ?> (alias: <?php Goal. ?>) expression to execute Prolog (of course).

By default, all output from these blocks will be escaped to avoid XSS attacks. You can also use unsafe queries, see below.

Unsafe queries: <?unsafe Goal. ?>

You can use the <?unsafe Goal. ?> expression to execute Prolog code without escaping its output (dangerous!). Avoid using this if you can.

For example, this renders a table of the numbers 1-10 and their squares:

<h3>Math</h3>
<table>
	<tr><th>N</th><th>Nยฒ</th></tr>
	<?unsafe
		bagof([N, Square], (between(1, 10, N), Square is N^2)), Flags),
		maplist(format("<tr><td>~w</td><td>~w</td></tr>"), Flags)
	?>
</table>

if block: <?if Goal. ?> ... <?end ?>

<?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 ?>

<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. ?>

<?* member(X, [1, 2, 3]), write(X). ?>

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

Echo: <?=Var Goal. ?>

1+1 = <?=X X is 1+1. ?>
<input type="text" name="ask" value="<?=Param query_param(ask, Param) ?>">

Works the same as query, but echoes (writes) the variable bound to Var. Escapes output.

Assertions: <? Program ?>

<?
% declare rules and facts
best_web_framework(php).
good_enough(X) :- between(1, 640, X).

% "directives" get executed upon evaluation
:- echo("hello!").
:- succ(68, X), write(X).
?>

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)

env/2

Envrionment variables.

query_param/2

Query parameters (from URL).

module(php)

php//1

PHP grammar.

phpinfo/0

phpinfo.

Writes HTML output, dumping the current environment and flags.

render/1

render(+Filename).

Renders the given PHP file.

html_escape/2

html_escape(-Raw:string, +Sanitized:string).

Escapes string using htmlspecialentities//1.

FAQ

Why?

Why not?

Is this real?

phpinfo/0 output

php's People

Contributors

guregu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

unusualjon

php's Issues

static files

currently cgi-bin is set to root ;)

added public_html but no static file serving yet

Actual control structures

In elephant PHP, you can do this:

<? if ($foo) { ?>
<b>it's true</b>
<? } else { ?>
nope
<? } ?>

This is very useful for looping and stuff. I'd like to add this but I'm not sure what the best syntax would be.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.