Beginnings of loops
This commit is contained in:
parent
5fdd28fe0e
commit
7213f4a487
14
dee-mode.el
14
dee-mode.el
@ -2,12 +2,6 @@
|
||||
|
||||
(add-to-list 'auto-mode-alist '("\\.dee\\'" . dee-mode))
|
||||
|
||||
(defface dee-function-param-face
|
||||
'((t . (:slant italic
|
||||
:weight bold
|
||||
:foreground "orange")))
|
||||
"A face to help identify function parameter in Deelang.")
|
||||
|
||||
(defcustom dee-indent-offset 4
|
||||
"Default indentation offset for Deelang."
|
||||
:group 'dee
|
||||
@ -34,14 +28,14 @@
|
||||
(defvar dee-font-lock-keywords
|
||||
`(,(rx symbol-start
|
||||
(or
|
||||
"if" "elif" "else" "")
|
||||
"if" "elif" "else" "loop" "until" "over" "in" "")
|
||||
symbol-end)
|
||||
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "<-")
|
||||
(1 font-lock-variable-name-face))
|
||||
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "->")
|
||||
(1 'dee-function-param-face))
|
||||
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "(")
|
||||
(1 font-lock-function-name-face))))
|
||||
(1 font-lock-function-name-face))
|
||||
(,(rx (group (regexp dee-font-lock-id-re)) (* space) "->")
|
||||
(1 font-lock-constant-face))))
|
||||
|
||||
(defun dee-indent-line-function ()
|
||||
'noindent) ;TODO
|
||||
|
@ -8,7 +8,7 @@ foo <- ->
|
||||
bar()
|
||||
baz()
|
||||
# functions with block statements
|
||||
oo <- x -> y -> x * y + 7 ## Multiple argument functions
|
||||
foo <- x -> y -> x * y + 7 ## Multiple argument functions
|
||||
if test?()
|
||||
do-something()
|
||||
elif other-test?()
|
||||
@ -23,3 +23,8 @@ else
|
||||
if second-test?()
|
||||
do-a-real-bad-thing()
|
||||
## Nested blocks
|
||||
|
||||
i <- 1
|
||||
loop until i > 10
|
||||
print(i)
|
||||
i <- 1
|
||||
|
7
peg.txt
7
peg.txt
@ -2,7 +2,8 @@ program := __* stmt*
|
||||
|
||||
stmt := assignment /
|
||||
funcall stop /
|
||||
conditional /
|
||||
conditional /
|
||||
loop /
|
||||
expr stop
|
||||
|
||||
expr := - _ expr /
|
||||
@ -31,6 +32,10 @@ if := "if" _ expr block
|
||||
elif := "elif" _ expr block
|
||||
else := "else" _ block
|
||||
|
||||
loop := "loop" _ (until / over / block)
|
||||
until := "until" _ expr block
|
||||
over := "over"_ id "in" _ expr block
|
||||
|
||||
block := expr / indented_block
|
||||
indented_block := stop indent __* stmt+ dedent
|
||||
|
||||
|
@ -107,6 +107,7 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) ->
|
||||
writeln!(w, "}}")?;
|
||||
}
|
||||
}
|
||||
parser::Stmt::Loop(_) => todo!()
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
3
src/internal.rs
Normal file
3
src/internal.rs
Normal file
@ -0,0 +1,3 @@
|
||||
//! Internal compiler steps, expansion, cps, optimization.
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
mod parser;
|
||||
mod evaluator;
|
||||
mod emitter;
|
||||
mod internal;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
|
@ -5,6 +5,7 @@ pub enum Stmt {
|
||||
Assignment(String, Expr),
|
||||
Funcall(Expr),
|
||||
Conditional(Vec<GuardedBlock>, Option<Block>),
|
||||
Loop(Loop),
|
||||
BareExpr(Expr),
|
||||
}
|
||||
|
||||
@ -49,6 +50,13 @@ pub struct GuardedBlock {
|
||||
pub block: Block,
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone)]
|
||||
pub enum Loop {
|
||||
Bare(Block),
|
||||
Until(GuardedBlock),
|
||||
Over(String, Expr, Block),
|
||||
}
|
||||
|
||||
peg::parser! {
|
||||
grammar deelang_parser() for str {
|
||||
pub rule program() -> Vec<Stmt>
|
||||
@ -57,6 +65,7 @@ peg::parser! {
|
||||
= a:assignment() { a } /
|
||||
f:funcall() stop() { Stmt::Funcall(f) } /
|
||||
c:conditional() { c } /
|
||||
l:_loop() { Stmt::Loop(l) } /
|
||||
e:expr() stop() { Stmt::BareExpr(e) }
|
||||
rule expr() -> Expr = precedence! {
|
||||
e1:(@) "=" _ e2:@ { Expr::Equal(Box::new(e1), Box::new(e2)) }
|
||||
@ -114,6 +123,17 @@ peg::parser! {
|
||||
}
|
||||
rule _else() -> Block
|
||||
= "else" _ b:block() { b }
|
||||
rule _loop() -> Loop
|
||||
= "loop" _ "until" _ e:expr() b:block() __* {
|
||||
Loop::Until(GuardedBlock{
|
||||
guard:e,
|
||||
block: b,
|
||||
})
|
||||
} /
|
||||
"loop" _ "over" _ i:id() "in" _ e:expr() b:block() __* {
|
||||
Loop::Over(i, e, b)
|
||||
} /
|
||||
"loop" _ b:block() __* { Loop::Bare(b) }
|
||||
rule block() -> Block
|
||||
= i:indented_block() { i } /
|
||||
e:expr() { vec![Stmt::BareExpr(e)] }
|
||||
|
Loading…
Reference in New Issue
Block a user