Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GLF Proyecto
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Automate
Agent sessions
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
marruiz
GLF Proyecto
Commits
32abaa0f
Commit
32abaa0f
authored
Jun 10, 2020
by
mandeca
Browse files
Options
Downloads
Patches
Plain Diff
AST
parent
adb457dc
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ast.c
+64
-72
64 additions, 72 deletions
ast.c
ast.h
+17
-0
17 additions, 0 deletions
ast.h
with
81 additions
and
72 deletions
ast.c
+
64
−
72
View file @
32abaa0f
...
...
@@ -16,9 +16,9 @@ extern char programName[];
ast_t
*
newLeafString
(
unsigned
tag
,
char
*
str
)
{
mallocCheck
(
ast_t
*
res
,
sizeof
(
ast_t
));
lnum
(
res
)
=
(
unsigned
)
lineNum
;
tag
(
res
)
=
tag
;
sv
(
res
)
=
str
;
res
->
lineN
=
(
unsigned
)
lineNum
;
res
->
tag
=
tag
;
res
->
u
.
str
=
str
;
return
res
;
}
...
...
@@ -26,57 +26,49 @@ ast_t *newLeafString(unsigned tag, char *str)
ast_t
*
newLeafNum
(
unsigned
tag
,
double
dval
)
{
mallocCheck
(
ast_t
*
res
,
sizeof
(
ast_t
));
lnum
(
res
)
=
(
unsigned
)
lineNum
;
tag
(
res
)
=
tag
;
dv
(
res
)
=
dval
;
res
->
lineN
=
(
unsigned
)
lineNum
;
res
->
tag
=
tag
;
res
->
u
.
real
=
dval
;
return
res
;
}
ast_t
*
newNode
(
unsigned
tag
,
ast_t
*
l
,
ast_t
*
r
)
{
mallocCheck
(
ast_t
*
res
,
sizeof
(
ast_t
));
lnum
(
res
)
=
(
unsigned
)
lineNum
;
tag
(
res
)
=
tag
;
left
(
res
)
=
l
;
r
ight
(
res
)
=
r
;
res
->
lineN
=
(
unsigned
)
lineNum
;
res
->
tag
=
tag
;
res
->
u
.
child
.
left
=
l
;
r
es
->
u
.
child
.
right
=
r
;
return
res
;
}
ast_t
*
newRoot
(
unsigned
tag
,
ast_t
*
lst
,
ast_t
*
nd
)
{
ast_t
*
newRoot
(
unsigned
tag
,
ast_t
*
lst
,
ast_t
*
nd
)
{
if
(
lst
==
NULL
)
{
if
(
nd
==
NULL
)
{
return
NULL
;
}
return
mkNd
(
tag
,
nd
,
NULL
);
return
newNode
(
tag
,
nd
,
NULL
);
}
if
(
nd
==
NULL
)
{
return
lst
;
}
ast_t
*
tmp
=
lst
;
while
(
right
(
tmp
)
!=
NULL
)
{
tmp
=
right
(
tmp
)
;
while
(
tmp
->
u
.
child
.
right
!=
NULL
)
{
tmp
=
tmp
->
u
.
child
.
right
;
}
right
(
tmp
)
=
mkNd
(
tag
,
nd
,
NULL
);
tmp
->
u
.
child
.
right
=
newNode
(
tag
,
nd
,
NULL
);
return
lst
;
}
static
double
expr
(
ast_t
*
root
)
{
switch
(
tag
(
root
))
{
case
'?'
:
if
(
expr
(
left
(
root
))
!=
0
.
0
)
static
double
evaluateExpr
(
ast_t
*
root
)
{
return
expr
(
left
(
right
(
root
)));
}
else
switch
(
root
->
tag
)
{
return
expr
(
right
(
right
(
root
)));
}
case
EQ
:
if
(
e
xpr
(
left
(
root
))
==
expr
(
right
(
root
)
))
{
if
(
e
valuateExpr
(
root
->
u
.
child
.
left
)
==
evaluateExpr
(
root
->
u
.
child
.
right
))
{
return
1
.
0
;
}
else
...
...
@@ -85,7 +77,7 @@ static double expr(ast_t *root) {
}
case
NE
:
if
(
e
xpr
(
left
(
root
))
!=
expr
(
right
(
root
)
))
if
(
e
valuateExpr
(
root
->
u
.
child
.
left
)
!=
evaluateExpr
(
root
->
u
.
child
.
right
))
{
return
1
.
0
;
}
...
...
@@ -95,7 +87,7 @@ static double expr(ast_t *root) {
}
case
LT
:
if
(
e
xpr
(
left
(
root
))
<
expr
(
right
(
root
)
))
if
(
e
valuateExpr
(
root
->
u
.
child
.
left
)
<
evaluateExpr
(
root
->
u
.
child
.
right
))
{
return
1
.
0
;
}
...
...
@@ -105,7 +97,7 @@ static double expr(ast_t *root) {
}
case
GT
:
if
(
e
xpr
(
left
(
root
))
>
expr
(
right
(
root
)
))
if
(
e
valuateExpr
(
root
->
u
.
child
.
left
)
>
evaluateExpr
(
root
->
u
.
child
.
right
))
{
return
1
.
0
;
}
...
...
@@ -115,134 +107,134 @@ static double expr(ast_t *root) {
}
case
'+'
:
return
e
xpr
(
left
(
root
))
+
expr
(
right
(
root
)
);
return
e
valuateExpr
(
root
->
u
.
child
.
left
)
+
evaluateExpr
(
root
->
u
.
child
.
right
);
case
'-'
:
if
(
left
(
root
)
==
NULL
)
if
(
root
->
u
.
child
.
left
==
NULL
)
{
return
-
e
xpr
(
right
(
root
)
);
return
-
e
valuateExpr
(
root
->
u
.
child
.
right
);
}
else
{
return
e
xpr
(
left
(
root
))
-
expr
(
right
(
root
)
);
return
e
valuateExpr
(
root
->
u
.
child
.
left
)
-
evaluateExpr
(
root
->
u
.
child
.
right
);
}
case
'*'
:
return
e
xpr
(
left
(
root
))
*
expr
(
right
(
root
)
);
return
e
valuateExpr
(
root
->
u
.
child
.
left
)
*
evaluateExpr
(
root
->
u
.
child
.
right
);
case
'/'
:
return
e
xpr
(
left
(
root
))
/
expr
(
right
(
root
)
);
return
e
valuateExpr
(
root
->
u
.
child
.
left
)
/
evaluateExpr
(
root
->
u
.
child
.
right
);
case
'%'
:
return
fmod
(
e
xpr
(
left
(
root
)),
expr
(
right
(
root
)
));
return
fmod
(
e
valuateExpr
(
root
->
u
.
child
.
left
),
evaluateExpr
(
root
->
u
.
child
.
right
));
case
DIV
:
return
(
int
)(
e
xpr
(
left
(
root
))
/
expr
(
right
(
root
)
))
:
return
(
int
)(
e
valuateExpr
(
root
->
u
.
child
.
left
)
/
evaluateExpr
(
root
->
u
.
child
.
right
))
:
case
'^'
:
return
pow
(
e
xpr
(
left
(
root
)),
expr
(
right
(
root
)
));
return
pow
(
e
valuateExpr
(
root
->
u
.
child
.
left
),
evaluateExpr
(
root
->
u
.
child
.
right
));
case
FLOAT
:
return
dv
(
root
)
;
return
root
->
u
.
real
;
case
ID
:
return
read
(
sv
(
root
)
);
return
get
(
root
->
u
.
str
);
case
SIN
:
return
sin
(
e
xpr
(
left
(
root
)
));
return
sin
(
e
valuateExpr
(
root
->
u
.
child
.
left
));
case
COS
:
return
cos
(
e
xpr
(
left
(
root
)
));
return
cos
(
e
valuateExpr
(
root
->
u
.
child
.
left
));
case
TAN
:
return
tan
(
e
xpr
(
left
(
root
)
));
return
tan
(
e
valuateExpr
(
root
->
u
.
child
.
left
));
case
LN
:
return
log
(
e
xpr
(
left
(
root
)
));
return
log
(
e
valuateExpr
(
root
->
u
.
child
.
left
));
default:
fprintf
(
stderr
,
"%s(%d): error -- Etiqueta desconocida en expresión AST %u
\n
"
,
programName
,
lnum
(
root
),
tag
(
root
)
)
;
fprintf
(
stderr
,
"%s(%d): error -- Etiqueta desconocida en e
valuateE
xpresión AST %u
\n
"
,
programName
,
lnum
(
root
),
root
)
->
tag
;
break
;
}
}
static
void
proc
(
ast_t
*
root
)
static
void
evaluateNode
(
ast_t
*
root
)
{
switch
(
tag
(
root
)
)
switch
(
root
)
->
tag
{
case
'='
:
insertModify
(
sv
(
left
(
root
)),
expr
(
right
(
root
)
));
edit
(
root
->
u
.
child
.
left
->
u
.
str
,
evaluateExpr
(
root
->
u
.
child
.
right
));
break
;
case
PRINT
:
if
(
left
(
root
)
==
NULL
)
if
(
root
->
u
.
child
.
left
==
NULL
)
{
printf
(
"%g
\n
"
,
e
xpr
(
right
(
root
)
));
printf
(
"%g
\n
"
,
e
valuateExpr
(
root
->
u
.
child
.
right
));
}
else
if
(
r
ight
(
root
)
==
NULL
)
else
if
(
r
oot
->
u
.
child
.
right
==
NULL
)
{
puts
(
sv
(
left
(
root
))
);
puts
(
root
->
u
.
child
.
left
->
u
.
str
);
}
else
{
printf
(
"%s%g
\n
"
,
sv
(
left
(
root
)),
expr
(
right
(
root
)
));
printf
(
"%s%g
\n
"
,
root
->
u
.
child
.
left
->
u
.
str
,
evaluateExpr
(
root
->
u
.
child
.
right
));
}
break
;
case
READ
:
if
(
left
(
root
)
==
NULL
)
if
(
root
->
u
.
child
.
left
==
NULL
)
{
double
rval
;
scanf
(
"%lf"
,
&
rval
);
insertModify
(
sv
(
right
(
root
))
,
rval
);
edit
(
root
->
u
.
child
.
right
->
u
.
str
,
rval
);
}
else
{
double
rval
;
printf
(
"%s"
,
sv
(
left
(
root
))
);
printf
(
"%s"
,
root
->
u
.
child
.
left
->
u
.
str
);
scanf
(
"%lf"
,
&
rval
);
insertModify
(
sv
(
right
(
root
))
,
rval
);
edit
(
root
->
u
.
child
.
right
->
u
.
str
,
rval
);
}
break
;
case
WHILE
:
if
(
r
ight
(
root
)
==
NULL
)
if
(
r
oot
->
u
.
child
.
right
==
NULL
)
{
while
(
e
xpr
(
left
(
root
)
));
while
(
e
valuateExpr
(
root
->
u
.
child
.
left
));
}
else
{
while
(
e
xpr
(
left
(
root
)
))
while
(
e
valuateExpr
(
root
->
u
.
child
.
left
))
{
evaluate
(
right
(
root
)
);
evaluate
Node
(
root
->
u
.
child
.
right
);
}
}
break
;
case
IF
:
if
(
e
xpr
(
left
(
root
)
))
if
(
e
valuateExpr
(
root
->
u
.
child
.
left
))
{
evaluate
(
right
(
root
)
);
evaluate
Node
(
root
->
u
.
child
.
right
);
}
break
;
case
ELSE
:
if
(
!
(
e
xpr
(
left
(
root
)
)))
if
(
!
(
e
valuateExpr
(
root
->
u
.
child
.
left
)))
{
evaluate
(
right
(
root
)
);
evaluate
Node
(
root
->
u
.
child
.
right
);
}
break
;
default:
fprintf
(
stderr
,
"%s(%d): error -- Etiqueta desconocida en expresión AST %u
\n
"
,
programName
,
lnum
(
root
),
tag
(
root
)
)
;
fprintf
(
stderr
,
"%s(%d): error -- Etiqueta desconocida en e
valuateE
xpresión AST %u
\n
"
,
programName
,
lnum
(
root
),
root
)
->
tag
;
break
;
}
}
...
...
@@ -252,7 +244,7 @@ void evaluate(ast_t *root)
{
while
(
root
!=
NULL
)
{
proc
(
left
(
root
)
);
root
=
r
ight
(
root
)
;
evaluateNode
(
root
->
u
.
child
.
left
);
root
=
r
oot
->
u
.
child
.
right
;
}
}
This diff is collapsed.
Click to expand it.
ast.h
0 → 100644
+
17
−
0
View file @
32abaa0f
#ifndef __AST_H__
#define __AST_H__
typedef
struct
ast_s
{
unsigned
tag
;
unsigned
lineN
;
union
{
struct
{
ast_s
*
left
,
*
right
;
}
child
;
char
*
str
;
double
real
;
}
u
;
}
ast_t
;
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment