Convert "What's an AST?" to typst
This commit is contained in:
parent
497c85338e
commit
69a3dfab39
@ -1,43 +0,0 @@
|
||||
\documentclass[
|
||||
solutions,
|
||||
hidewarning,
|
||||
singlenumbering,
|
||||
nopagenumber
|
||||
]{../../../lib/tex/ormc_handout}
|
||||
\usepackage{../../../lib/tex/macros}
|
||||
|
||||
|
||||
\usepackage[linguistics]{forest}
|
||||
|
||||
|
||||
|
||||
\title{Warm-Up: What's an AST?}
|
||||
\uptitler{\smallurl{}}
|
||||
\subtitle{Prepared by Mark on \today. \\ Based on a true story.}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
Say you have a valid string of simple arithmetic that contains no unary operators (like $3!$ or $-4$) and no parenthesis:
|
||||
$$
|
||||
3 + 9 \times 8 \div 5 \land 6
|
||||
$$
|
||||
|
||||
You may assume that all numbers and operators in this string consist of exactly one character. \\
|
||||
|
||||
Devise an algorithm that turns this string into a tree (as shown below), respecting the order of operations $[\land, \times, \div, +, -]$.
|
||||
|
||||
\begin{center}
|
||||
\begin{forest}
|
||||
[$+$
|
||||
[3]
|
||||
[$\div$
|
||||
[$\times$[9][8]]
|
||||
[$\land$[5][6]]
|
||||
]
|
||||
]
|
||||
\end{forest}
|
||||
\end{center}
|
||||
|
||||
\end{document}
|
73
src/Warm-Ups/What's an AST/main.typ
Normal file
73
src/Warm-Ups/What's an AST/main.typ
Normal file
@ -0,0 +1,73 @@
|
||||
#import "@local/handout:0.1.0": *
|
||||
#import "@preview/cetz:0.3.1"
|
||||
|
||||
#show: handout.with(
|
||||
title: [Warm-Up: What's an AST?],
|
||||
by: "Mark",
|
||||
subtitle: "Based on a true story.",
|
||||
)
|
||||
|
||||
#problem()
|
||||
Say we have a valid string of simple arithmetic that contains \
|
||||
no unary operators (like $3!$ or $-4$) and no parenthesis:
|
||||
|
||||
#v(2mm)
|
||||
$
|
||||
3 + 9 times 8 div 5 and 6
|
||||
$
|
||||
#v(2mm)
|
||||
|
||||
You may assume that all numbers and operators in this string consist of exactly one character. \
|
||||
|
||||
Devise an algorithm that turns such strings into a tree (as shown below), \
|
||||
respecting the order of operations $[and, times, div, +, -]$.
|
||||
|
||||
#v(2mm)
|
||||
|
||||
#align(
|
||||
center,
|
||||
cetz.canvas({
|
||||
import cetz.draw: *
|
||||
|
||||
content((0, 0), $+$, name: "r")
|
||||
content((-0.5, -1), $3$, name: "a")
|
||||
content((0.5, -1), $div$, name: "b")
|
||||
|
||||
content((-0.3, -2), $times$, name: "ba")
|
||||
content((1.3, -2), $and$, name: "bb")
|
||||
|
||||
content((-0.8, -3), $9$, name: "baa")
|
||||
content((0.2, -3), $8$, name: "bab")
|
||||
content((0.8, -3), $5$, name: "bba")
|
||||
content((1.8, -3), $6$, name: "bbb")
|
||||
|
||||
// Zero-sized arrows are a hack for offset.
|
||||
set-style(
|
||||
stroke: (thickness: 0.3mm),
|
||||
mark: (
|
||||
start: (
|
||||
symbol: "|",
|
||||
offset: 0.25,
|
||||
width: 0mm,
|
||||
length: 0mm,
|
||||
),
|
||||
end: (
|
||||
symbol: "|",
|
||||
offset: 0.25,
|
||||
width: 0mm,
|
||||
length: 0mm,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
line("r", "a")
|
||||
line("r", "b")
|
||||
line("b", "ba")
|
||||
line("b", "bb")
|
||||
line("ba", "baa")
|
||||
line("ba", "bab")
|
||||
line("bb", "bba")
|
||||
line("bb", "bbb")
|
||||
}),
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user