#!/usr/bin/perl -w

use v5.36;
use strict;

my %expr;
my %val;
while (<>) {
	chomp;
	my ($name, $rest) = split /: /;
	my @ops = split /\s+/, $rest;
	if (@ops > 1) {
		$expr{$name} = \@ops;
	} else {
		$val{$name} = $ops[0];
	}
}

sub walk($var) {
	return $val{$var} if defined $val{$var};
	return undef if !defined $expr{$var};
	my ($arg1, $op, $arg2) = @{ $expr{$var} };
	my $val1 = walk($arg1);
	my $val2 = walk($arg2);
	return undef if !defined $val1 || !defined $val2;
	if ($op eq '+') {
		return $val{$var} = $val1 + $val2;
	} elsif ($op eq '-') {
		return $val{$var} = $val1 - $val2;
	} elsif ($op eq '*') {
		return $val{$var} = $val1 * $val2;
	} elsif ($op eq '/') {
		return $val{$var} = $val1 / $val2;
	}
}


delete $val{humn};
delete $expr{humn};
walk('root');
my $val = $val{$expr{root}[2]};
my $var = $expr{root}[0];

while ($var ne 'humn') {
	say "$var = $val, expr = @{ $expr{$var} }";
	my ($arg1, $op, $arg2) = @{ $expr{$var} };
	$arg1 = $val{$arg1} if defined $val{$arg1};
	$arg2 = $val{$arg2} if defined $val{$arg2};
	if ($arg2 =~ /\d/) {
		$var = $arg1;
		if ($op eq '/') {
			$val = $val * $arg2;
		} elsif ($op eq '*') {
			$val = $val / $arg2;
		} elsif ($op eq '+') {
			$val = $val - $arg2;
		} elsif ($op eq '-') {
			$val = $val + $arg2;
		}
	} elsif ($arg1 =~ /\d/) {
		$var = $arg2;
		if ($op eq '/') {
			$val = $arg1 / $val;
		} elsif ($op eq '*') {
			$val = $val / $arg1;
		} elsif ($op eq '+') {
			$val = $val - $arg1;
		} elsif ($op eq '-') {
			$val = $arg1 - $val;
		}
	} else {
		last;
	}
	say "var $var = $val";
	$val{$var} = $val;
}

say $val{humn};
