Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Proof Assistant with Solver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Zhang, Jenny
Proof Assistant with Solver
Commits
bb42360e
Commit
bb42360e
authored
9 months ago
by
Jenny Zhang
Browse files
Options
Downloads
Patches
Plain Diff
add astToLaTeX function and test for expression
parent
b364dc71
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/utils/ast.js
+45
-0
45 additions, 0 deletions
src/utils/ast.js
tests/unit/ast.test.js
+51
-0
51 additions, 0 deletions
tests/unit/ast.test.js
with
96 additions
and
0 deletions
src/utils/ast.js
+
45
−
0
View file @
bb42360e
...
@@ -62,6 +62,50 @@ function astToString(node, parentPrecedence = 0) {
...
@@ -62,6 +62,50 @@ function astToString(node, parentPrecedence = 0) {
return
result
;
return
result
;
}
}
function
astToLaTeX
(
node
,
parentPrecedence
=
0
)
{
if
(
!
node
)
return
""
;
let
currentPrecedence
=
precedence
[
node
.
type
];
let
left
=
astToLaTeX
(
node
.
left
,
currentPrecedence
);
let
right
=
astToLaTeX
(
node
.
right
,
currentPrecedence
);
let
result
;
switch
(
node
.
type
)
{
case
'
not
'
:
result
=
`
\\
neg
${
left
}
`
;
break
;
case
'
and
'
:
result
=
`
${
left
}
\\
land
${
right
}
`
;
break
;
case
'
or
'
:
result
=
`
${
left
}
\\
lor
${
right
}
`
;
break
;
case
'
imply
'
:
result
=
`
${
left
}
\\
rightarrow
${
right
}
`
;
break
;
case
'
symbol
'
:
result
=
`
\\
mathit{
${
node
.
value
}
}`
;
break
;
case
'
true
'
:
result
=
'
\\
top
'
;
break
;
case
'
false
'
:
result
=
'
\\
bot
'
;
break
;
default
:
throw
new
Error
(
`Unknown node type:
${
node
.
type
}
`
);
}
if
(
currentPrecedence
<
parentPrecedence
)
{
result
=
`(
${
result
}
)`
;
}
return
result
;
}
var
ast
=
{};
var
ast
=
{};
ast
.
Not
=
Node
.
bind
(
null
,
'
not
'
);
ast
.
Not
=
Node
.
bind
(
null
,
'
not
'
);
...
@@ -72,5 +116,6 @@ ast.Symbol = Node.bind(null, 'symbol');
...
@@ -72,5 +116,6 @@ ast.Symbol = Node.bind(null, 'symbol');
ast
.
True
=
Node
.
bind
(
null
,
'
true
'
);
ast
.
True
=
Node
.
bind
(
null
,
'
true
'
);
ast
.
False
=
Node
.
bind
(
null
,
'
false
'
);
ast
.
False
=
Node
.
bind
(
null
,
'
false
'
);
ast
.
astToString
=
astToString
;
ast
.
astToString
=
astToString
;
ast
.
astToLaTeX
=
astToLaTeX
;
module
.
exports
=
ast
;
// Export the AST module
module
.
exports
=
ast
;
// Export the AST module
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/unit/ast.test.js
+
51
−
0
View file @
bb42360e
...
@@ -105,3 +105,54 @@ test('astToString could work correctly for complex expression', () => {
...
@@ -105,3 +105,54 @@ test('astToString could work correctly for complex expression', () => {
expect
(
output
).
toEqual
(
inputString
);
expect
(
output
).
toEqual
(
inputString
);
});
});
// astToLaTeX test for expression
test
(
'
astToLaTeX could work correctly for and expression
'
,
()
=>
{
const
inputString
=
"
A & B
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
mathit{A}
\\
land
\\
mathit{B}
"
);
});
test
(
'
astToLaTeX could work correctly for or expression
'
,
()
=>
{
const
inputString
=
"
A || B
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
mathit{A}
\\
lor
\\
mathit{B}
"
);
});
test
(
'
astToLaTeX could work correctly for negation expression
'
,
()
=>
{
const
inputString
=
"
!A
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
neg
\\
mathit{A}
"
);
});
test
(
'
astToLaTeX could work correctly for implication expression
'
,
()
=>
{
const
inputString
=
"
A -> B
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
mathit{A}
\\
rightarrow
\\
mathit{B}
"
);
});
test
(
'
astToLaTeX could work correctly for true and false expression
'
,
()
=>
{
const
inputString
=
"
true & false
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
top
\\
land
\\
bot
"
);
});
test
(
'
astToLaTeX could work correctly for mixed logical expression
'
,
()
=>
{
const
inputString
=
"
P -> Q || !R
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
mathit{P}
\\
rightarrow
\\
mathit{Q}
\\
lor
\\
neg
\\
mathit{R}
"
);
});
test
(
'
astToLaTeX could work correctly for complex expression
'
,
()
=>
{
const
inputString
=
"
X & (Y -> Z)
"
;
const
input
=
parse
(
inputString
);
const
output
=
ast
.
astToLaTeX
(
input
);
expect
(
output
).
toEqual
(
"
\\
mathit{X}
\\
land (
\\
mathit{Y}
\\
rightarrow
\\
mathit{Z})
"
);
});
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
register
or
sign in
to comment