Slide 1

Slide 1 text

3VTU-5ʙ͍·࢖͏ʂ3VTUʙ ΠϯλϓϦλΛ࡞ͬͯ·ͳͿ 3VTUΒ͍͠ॻ͖ํ !IFMMPZVL

Slide 2

Slide 2 text

ࣗݾ঺հ ͱΑͩΏ͏͖ αΠόʔΤʔδΣϯτ ΞυςΫελδΦॴଐ ීஈ͸ 4DBMBΤϯδχΞ 3VTU޷͖ ݴޠॲཧܥ޷͖ ฒྻॲཧ޷͖ !IFMMPZVL

Slide 3

Slide 3 text

ࠓ೔࿩͢͜ͱ ΠϯλϓϦλͱ͸ʁ 3VCZ͔Β 3VTU΁

Slide 4

Slide 4 text

ΠϯλϓϦλΛ࡞ͬͨ͜ͱͷ͋Δํ !

Slide 5

Slide 5 text

ιʔείʔυΛղऍͯ͠ந৅ߏจ໦Λ࡞Δ ໦Λղੳ࣮ͯ͠ߦ಺༰ΛධՁ͢Δ ΠϯλϓϦλͱ͸ʁ ۩ମతʹԿΛ͢Δ΋ͷʁ

Slide 6

Slide 6 text

଍͠ࢉͱֻ͚ࢉɼ਺஋ͷେখൺֱ ܭࢉͷ༏ઌॱҐ͸ߟ͑ͳ͍ ม਺΋ѻ͑Δ ਓྗͰߏจ໦Λ౉͢ɼύʔαʔΛ࡞Βͳ͍ ؆໿ΛԿճ͔ىͯ݁͜͠ՌΛಘΔ ΠϯλϓϦλͱ͸ʁ ࠓճͭ͘ΔΠϯλϓϦλ

Slide 7

Slide 7 text

ந৅ߏจ໦ʁ !

Slide 8

Slide 8 text

ΠϯλϓϦλͱ͸ʁ ந৅ߏจ໦ͬͯͲ͏͍͏΋ͷʁ ιʔείʔυΛղऍͯ͠τʔΫϯʹ௚͢ ௚͞ΕͨτʔΫϯ͸໦ߏ଄ʹͳ͍ͬͯΔ

Slide 9

Slide 9 text

ΠϯλϓϦλͱ͸ʁ ந৅ߏจ໦ͬͯͲ͏͍͏΋ͷʁ

Slide 10

Slide 10 text

ΠϯλϓϦλͱ͸ʁ ͨͱ͑͹࣮૷͢Δͱ͜Μͳײ͡ Add( Multiply(Number(1), Number(2)), Multiply(Number(3), Number(4)), );

Slide 11

Slide 11 text

ΠϯλϓϦλͱ͸ʁ ࠓճͭ͘ΔτʔΫϯ ଍͠ࢉ"EE ֻ͚ࢉ.VMUJQMZ ΑΓখ͍͞-FTT5IBO ਺஋/VNCFS ม਺7BS ਅِ஋#PPM7BMVF

Slide 12

Slide 12 text

؆໿ʁ !

Slide 13

Slide 13 text

ؔ਺Λద༻ͯ݁͠ՌΛ·ͱΊΔ͜ͱ ׬ྃ৚݅Λຬͨ͢·ͰԿ౓΋؆໿͢Δ ΠϯλϓϦλͱ͸ʁ ؆໿ͱ͸ʁ

Slide 14

Slide 14 text

؆໿ ؆໿ ؆໿ ΠϯλϓϦλͱ͸ʁ ਺ࣜΛ؆໿ͯ͠ܭࢉ͢Δͱ

Slide 15

Slide 15 text

ΠϯλϓϦλͱ͸ʁ ࠓճͷαϯϓϧϓϩάϥϜͩͱ // impl Token ͷதͰ͢ pub fn reduce(&self, env: &HashMap) -> Token { use Token::*; match self { (…) &Add(ref blv, ref brv) if blv.is_reducible() => { Add(Box::new(blv.reduce(env)), brv.clone()) } &Add(ref blv, ref brv) if brv.is_reducible() => { Add(blv.clone(), Box::new(brv.reduce(env))) } &Add(ref blv, ref brv) => match **blv { Number(left_value) => match **brv { Number(right_value) => Number(left_value + right_value), _ => panic!("Unexpected error in Add!"), }, _ => panic!("Unexpected error in Add!"), }, (…)

Slide 16

Slide 16 text

3VCZ͔Β 3VTU΁

Slide 17

Slide 17 text

3VCZ͔Β 3VTU΁ 3VCZͷίʔυΛ 3VTUʹ௚ͯ͠ΈΑ͏ ΏΔ;ΘͰ͍͖·͢!

Slide 18

Slide 18 text

3VCZ͔Β 3VTU΁ 3VCZͷίʔυΛ 3VTUͰॻ͘ͱʜ DMBTTWTFOVN ࠶ؼॲཧͷѻ͍ํ ݁ՌΛҰ࣌อଘ͍ͨ͠ͱ͖

Slide 19

Slide 19 text

3VTUͷ FOVN ͸୅਺తσʔλܕʹͳͬͯ ͓ΓɼύλʔϯϚονͰ͖Δ 3VCZ͔Β 3VTU΁ DMBTTWTFOVN

Slide 20

Slide 20 text

ͳͷͰɼ Ͳ͜Λ୅਺తσʔλܕͰ࣮૷Ͱ͖Δ͔Λ·ͣ ߟ͑ΔͱɼεοΩϦ࣮ͨ͠૷ʹͳΓ΍͍͢! 3VCZ͔Β 3VTU΁ DMBTTWTFOVN

Slide 21

Slide 21 text

3VCZ͔Β 3VTU΁ 3VCZͰ͸ DMBTTͰදݱ͢Δ͕ʜ class Number < Struct.new(:value) end class Var < Struct.new(:value) end class Add < Struct.new(:left, :right) end class Multiply < Struct.new(:left, :right) end class LessThan < Struct.new(:left, :right) end class BoolValue < Struct.new(:bool) end

Slide 22

Slide 22 text

3VCZ͔Β 3VTU΁ 3VTUͰ͸ FOVN ͰදݱͰ͖Δ #[derive(Clone)] pub enum Token { Number(i32), BoolValue(bool), Var(String), Add(Box, Box), Multiply(Box, Box), LessThan(Box, Box), }

Slide 23

Slide 23 text

3VCZ͔Β 3VTU΁ 3VCZ͸ؔ਺Λ DMBTTʹॻ͘ class Number < Struct.new(:value) def reducible? false end end class Add < Struct.new(:left, :right) def reducible? true end end (…)

Slide 24

Slide 24 text

3VCZ͔Β 3VTU΁ 3VTU͸ύλʔϯϚον͕࢖͑Δ impl Token { pub fn is_reducible(&self) -> bool { use Token::*; match *self { Number(_) => false, BoolValue(_) => false, Var(_) => true, Add(_, _) => true, Multiply(_, _) => true, LessThan(_, _) => true, } } }

Slide 25

Slide 25 text

#PY5 3VCZ͔Β 3VTU΁ ࠶ؼॲཧͷѻ͍ํ

Slide 26

Slide 26 text

ώʔϓ্ͷσʔλʹର͢ΔϙΠϯλ #PYΛ࢖͏͜ͱͰώʔϓʹσʔλΛอଘ 3VCZ͔Β 3VTU΁ #PY5

Slide 27

Slide 27 text

3VTU͸ίϯύΠϧ࣌ʹܕͷαΠζΛܾΊΔ ͔͠͠࠶ؼߏ଄͸αΠζ͕ܾ·Βͳ͍ #PYΛ࢖͏ͱαΠζΛܾఆͰ͖Δ 3VCZ͔Β 3VTU΁ ࠶ؼߏ଄ʹ#PY5͕ඞཁͳཧ༝

Slide 28

Slide 28 text

3VCZ͔Β 3VTU΁ ଟ͘ͷݴޠͰ͸࠶ىߏ଄΋ී௨ʹѻ͑Δ class Number < Struct.new(:value) end class Var < Struct.new(:value) end class Add < Struct.new(:left, :right) end class Multiply < Struct.new(:left, :right) end class LessThan < Struct.new(:left, :right) end class BoolValue < Struct.new(:bool) end

Slide 29

Slide 29 text

3VCZ͔Β 3VTU΁ ଟ͘ͷݴޠͰ͸࠶ىߏ଄΋ී௨ʹѻ͑Δ class Number < Struct.new(:value) end class Var < Struct.new(:value) end class Add < Struct.new(:left, :right) end class Multiply < Struct.new(:left, :right) end class LessThan < Struct.new(:left, :right) end class BoolValue < Struct.new(:bool) end ࣮ߦ͔ͯ͠Β ελοΫΦʔόʔϑϩʔͨ͠Γ͢Δ

Slide 30

Slide 30 text

3VCZ͔Β 3VTU΁ 3VTUͰ͸ #PY5Λ༻͍Δඞཁ͋Γ #[derive(Clone)] pub enum Token { Number(i32), BoolValue(bool), Var(String), Add(Box, Box), Multiply(Box, Box), LessThan(Box, Box), }

Slide 31

Slide 31 text

NVU TFMG 3VCZ͔Β 3VTU΁ ݁ՌΛҰ࣌อଘ͍ͨ͠ͱ͖

Slide 32

Slide 32 text

3VCZ͔Β 3VTU΁ ঢ়ଶͷอଘॲཧ͸Ͳ͏ͨ͠Βʜ class Machine < Struct.new(:expression) def step self.expression = expression.reduce end def run while expression.reducible? puts expression step end puts expression end end

Slide 33

Slide 33 text

3VCZ͔Β 3VTU΁ NVU Λ࢖ͬͯ௚ͯ͠ΈΔ impl Machine { (…) pub fn run(&mut self) { let environment = HashMap::new(); while self.expression.is_reducible() { println!("{}", &self.expression); self.step(&environment); } println!("{}", &self.expression); } fn step(&mut self, environment: &HashMap) { self.expression = self.expression.reduce(environment); } }

Slide 34

Slide 34 text

·ͱΊ ୅਺తσʔλܕΛ্खʹ࢖͓͏ ࠶ؼॲཧ΍࠶ؼߏ଄ʹ͸एׯ஫ҙ͕ඞཁ https://github.com/yuk1ty/simple-interpreter-rs ˞3VCZͷίʔυ͸ޙ೔্͛·͢ʂ

Slide 35

Slide 35 text

3VTU-5ʙ͍·࢖͏ʂ3VTUʙ ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠