about summary refs log tree commit diff stats
path: root/html/factorial.mu.html
blob: 54a70688314c4bdafbc5520ef85c7e9ba5bd5ddb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - factorial.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muScenario { color: #00af00; }
.Delimiter { color: #a04060; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.muControl { color: #c0a020; }
.muRecipe { color: #ff8700; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># example program: compute the factorial of 5</span>

<span class="muRecipe">recipe</span> main [
  <span class="Constant">local-scope</span>
  x:number<span class="Special"> &lt;- </span>factorial <span class="Constant">5:literal</span>
  $print <span class="Constant">[result: ]</span>, x:number, <span class="Constant">[ </span>
<span class="Constant">]</span>
]

<span class="muRecipe">recipe</span> factorial [
  <span class="Constant">local-scope</span>
  n:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># if n=0 return 1</span>
    zero?:boolean<span class="Special"> &lt;- </span>equal n:number, <span class="Constant">0:literal</span>
    <span class="muControl">break-unless</span> zero?:boolean
    <span class="muControl">reply</span> <span class="Constant">1:literal</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># return n * factorial(n-1)</span>
  x:number<span class="Special"> &lt;- </span>subtract n:number, <span class="Constant">1:literal</span>
  subresult:number<span class="Special"> &lt;- </span>factorial x:number
  result:number<span class="Special"> &lt;- </span>multiply subresult:number, n:number
  <span class="muControl">reply</span> result:number
]

<span class="Comment"># unit test</span>
<span class="muScenario">scenario</span> factorial-test [
  run [
    1:number<span class="Special"> &lt;- </span>factorial <span class="Constant">5:literal</span>
  ]
  memory-should-contain [
    1<span class="Special"> &lt;- </span>120
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
t; 2023-10-14 12:04:51 +0200 layout: get rid of baseline, firstBaseline functions' href='/ahoang/chawan/commit/src/layout/box.nim?id=95c5438a619783230b26d0ebf18a56f425c3dbf5'>95c5438a ^
191d7179 ^
a8f38915 ^






200a3784 ^




















a8f38915 ^
200a3784 ^



















191d7179 ^
200a3784 ^
191d7179 ^
6078b9ca ^


191d7179 ^


efbbe82f ^











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                    
                     
                 
                        

    

                            
 


                                            
 
                        
                                                 

                          
                   

                           
                  
             
               
                  
                      
                         

                  
 
                                   
                               
                   



                                    


                           
 



                                             


                            

                   
               
 
                               

                                                                           
                           


                                  

                                
                              




                                
 




                                            
 
                               
                   

                                                
                                         


                                                                           
                          



                                                       
 






                                




















                                                          
                                    



















                                                      
                                 
                                             
 


                                             


                                      











                                           
import css/cssvalues
import css/stylednode
import img/bitmap
import layout/layoutunit

type
  DimensionType* = enum
    dtHorizontal, dtVertical

  Offset* = array[DimensionType, LayoutUnit]

  Size* = array[DimensionType, LayoutUnit]

  InlineAtomType* = enum
    iatSpacing, iatWord, iatInlineBlock, iatImage

  InlineAtom* = ref object
    offset*: Offset
    size*: Size
    case t*: InlineAtomType
    of iatSpacing:
      discard
    of iatWord:
      str*: string
    of iatInlineBlock:
      innerbox*: BlockBox
    of iatImage:
      bmp*: Bitmap

  RootInlineFragmentState* = object
    # offset relative to parent
    offset*: Offset
    # baseline of the first line box
    firstBaseline*: LayoutUnit
    # baseline of the last line box
    baseline*: LayoutUnit
    # minimum content width
    xminwidth*: LayoutUnit
    size*: Size

  RootInlineFragment* = ref object
    fragment*: InlineFragment # root fragment
    state*: RootInlineFragmentState

  SplitType* = enum
    stSplitStart, stSplitEnd

  Area* = object
    offset*: Offset
    size*: Size

  InlineFragmentState* = object
    startOffset*: Offset # offset of the first word, for position: absolute
    areas*: seq[Area] # background that should be painted by fragment
    atoms*: seq[InlineAtom]

  InlineFragment* = ref object
    children*: seq[InlineFragment]
    computed*: CSSComputedValues
    node*: StyledNode
    splitType*: set[SplitType]
    state*: InlineFragmentState
    text*: seq[string]
    newline*: bool #TODO enumify
    bmp*: Bitmap
    box*: BlockBox

  Span* = object
    start*: LayoutUnit
    send*: LayoutUnit

  RelativeRect* = array[DimensionType, Span]

  BlockBoxLayoutState* = object
    offset*: Offset
    size*: Size # padding size
    margin*: RelativeRect #TODO get rid of this?
    positioned*: RelativeRect #TODO ditto
    # very bad name. basically the minimum content width after the contents
    # have been positioned (usually the width of the shortest word.) used
    # in table cells.
    xminwidth*: LayoutUnit
    # baseline of the first line box of all descendants
    firstBaseline*: LayoutUnit
    # baseline of the last line box of all descendants
    baseline*: LayoutUnit

  BlockBox* = ref object
    computed*: CSSComputedValues
    node*: StyledNode
    inline*: RootInlineFragment
    nested*: seq[BlockBox]
    state*: BlockBoxLayoutState

func offset*(x, y: LayoutUnit): Offset =
  return [dtHorizontal: x, dtVertical: y]

func x*(offset: Offset): LayoutUnit {.inline.} =
  return offset[dtHorizontal]

func x*(offset: var Offset): var LayoutUnit {.inline.} =
  return offset[dtHorizontal]

func `x=`*(offset: var Offset; x: LayoutUnit) {.inline.} =
  offset[dtHorizontal] = x

func y*(offset: Offset): LayoutUnit {.inline.} =
  return offset[dtVertical]

func y*(offset: var Offset): var LayoutUnit {.inline.} =
  return offset[dtVertical]

func `y=`*(offset: var Offset; y: LayoutUnit) {.inline.} =
  offset[dtVertical] = y

func size*(w, h: LayoutUnit): Size =
  return [dtHorizontal: w, dtVertical: h]

func w*(size: Size): LayoutUnit {.inline.} =
  return size[dtHorizontal]

func w*(size: var Size): var LayoutUnit {.inline.} =
  return size[dtHorizontal]

func `w=`*(size: var Size; w: LayoutUnit) {.inline.} =
  size[dtHorizontal] = w

func h*(size: Size): LayoutUnit {.inline.} =
  return size[dtVertical]

func h*(size: var Size): var LayoutUnit {.inline.} =
  return size[dtVertical]

func `h=`*(size: var Size; h: LayoutUnit) {.inline.} =
  size[dtVertical] = h

func `+`*(a, b: Offset): Offset =
  return offset(x = a.x + b.x, y = a.y + b.y)

func `-`*(a, b: Offset): Offset =
  return offset(x = a.x - b.x, y = a.y - b.y)

proc `+=`*(a: var Offset; b: Offset) =
  a.x += b.x
  a.y += b.y

func left*(s: RelativeRect): LayoutUnit =
  return s[dtHorizontal].start

func right*(s: RelativeRect): LayoutUnit =
  return s[dtHorizontal].send

func top*(s: RelativeRect): LayoutUnit =
  return s[dtVertical].start

func bottom*(s: RelativeRect): LayoutUnit =
  return s[dtVertical].send